题解 | #寻找第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
        return findKth_(a,0,n-1,K);
    }
    public static int findKth_(int[] arry,int low,int hei,int k){
        /*if (hei-low<1){
            return;
        }*/
        int midValue = arry[low];
        int start = low;
        int end = hei;
        while (end>start){
            while (end>start&&midValue<arry[end]){
                end--;
            }
            if (start<end&&midValue>arry[end]){
                arry[start++]=arry[end];
            }
            while (start<end&&midValue>arry[start]){
                start++;
            }
            if (start<end){
                arry[end--]=arry[start];
            }

        }
        arry[end]=midValue;
       if (hei-end==k-1){
            return arry[hei-k+1];
        }else if(hei-end<k-1){
            return findKth_(arry,low,start-1,k-hei+start-1);
        }else{
            return findKth_(arry,start+1,hei,k);
        }
    }
}
全部评论

相关推荐

09-14 17:23
门头沟学院
故事和酒66:所以说副业很重要,程序员干到40岁,再怎么也赚300万了,吃吃利息也够活下去
点赞 评论 收藏
分享
牛客37185681...:马德,我感觉这是我面过最恶心的公司,一面是两个女hr,说什么实习前几个月属于试用期,试用期过了才能转成正式实习生,我***笑了,问待遇就是不说,问能不能接受全栈,沙币公司
如果可以选,你最想去哪家...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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