• 数组中第三大的数 leetcode 414


    数组中第三大的数:

    class Solution {
        public int thirdMax(int[] nums) {
            int len = nums.length;
            int max = nums[0];
            int center = nums[0];
            int min = nums[0];
            int swp;
            int[] third = new int[3];
            int k= 0;
            boolean flag = false;
            for(int i=1;i<len;++i){
                if(nums[i]!=max&&!flag){
                    center = nums[i];
                    if(center>max){
                        swp = max;
                        max = center;
                        center = swp;
                    }
                    flag = true;
                }else if(nums[i]!=max&&nums[i]!=center&&flag){
                    min = nums[i];
                    if(min>max){
                        swp = max;
                        max = min;
                        min = center;
                        center = swp;
                    }else if(min>center){
                        swp = center;
                        center = min;
                        min = swp;
                    }else{
                        
                    }
                    k = i+1;
                    break;
                }else{
                    
                }
            }
            if(k!=0){
                for(int i=k;i<len;++i){
                    if(nums[i]<=min||nums[i]==center||nums[i]==max){
                        
                    }else if(nums[i]>max){
                        swp = max;
                        max = nums[i];
                        min = center;
                        center = swp;
                    }else if(nums[i]>center){
                        swp = center;
                        center = nums[i];
                        min = swp;
                    }else{
                        min = nums[i];
                    }
                }
                return min;
            }else{
                return max;
            }
        }
    }
  • 相关阅读:
    控制element表格禁用选择
    深度拷贝
    VScode修复eslint报错,保存的时候自动格式修正
    关于route监听
    PAT 1030 完美数列
    PAT1029 旧键盘(C完全正确)
    PAT 1028 人口普查
    PAT 1016
    PAT:1013
    PAT :1012 数字分类
  • 原文地址:https://www.cnblogs.com/erdanyang/p/11512216.html
Copyright © 2020-2023  润新知