My c code (0 ms)


  • 0
    Y
    void swap(int* a,int* b)
    {
        int temp=*a;
        *a=*b;
        *b=temp;
    }
    int partion(int* nums,int numsSize){
        int pos = 0,i=pos+1,j=numsSize-1;
        while(1){
                while(i<numsSize && nums[i]>nums[pos])
                        i++;
                while(j>=0 && nums[j]<nums[pos])
                        j--;
                if(i<j){
                        swap(&nums[i],&nums[j]);
                        i++;
                        j--;
                }
                else{
                        swap(&nums[i-1],&nums[pos]);
                        break;
                }
        }
        return i-1;
    }
    
    int findKthLargest(int* nums, int numsSize, int k) {
        if(numsSize==1)
                return nums[0];
        int pos = partion(nums,numsSize);
        if(k<(pos+1))
                findKthLargest(nums,pos,k);
        else if(k>(pos+1))
                findKthLargest(nums+pos+1,numsSize-pos-1,k-pos-1);
        else
                return nums[pos];
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.