zoukankan      html  css  js  c++  java
  • 数组中第三大的数 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;
            }
        }
    }
  • 相关阅读:
    How Many Answers Are Wrong
    Agri-Net —poj1258
    食物链
    A Bug's Life
    畅通工程
    Shortest path of the king
    Alex and Number
    KMP
    快速幂
    0x04
  • 原文地址:https://www.cnblogs.com/erdanyang/p/11512216.html
Copyright © 2011-2022 走看看