题解 | #寻找第K大#

寻找第K大

http://www.nowcoder.com/practice/e016ad9b7f0b45048c58a9f27ba618bf

import java.util.*;

public class Solution {
    public int findKth(int[] a, int n, int K) {
        // write code here
        Arrays.sort(a);
      	//记录当前的数是第几大
        int temp = 1;
      	//遍历数组a
        for(int i =n-1 ;i>=0;i--){
          	//如果前面有重复的 跳过当前循环
            if(i>0&&a[i]==a[i-1]){
                continue;
            }
          	//判断需要找的第K大是不是当前的第几大 是则返回
            if(temp==K){
                return a[i];
            }
          	//每次temp加1 
            temp++;
        }
      	//走完循环还没找则只能返回第一个数
        return a[0];
    }
}
全部评论

相关推荐

zephory:内容太乱了,根本捕捉不到重点,指导你会的很多,但是看不到具体的强项 个人技能宜精不宜多 项目那块太繁琐了,面试官或者hr只想知道你在项目中看了啥以及具体的收益
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务