Why runtime error, c++ code


  • 0
    L

    Can anyone tell me why my code is runtime error? I use quick select to solve this problem,

    '''class Solution {
    public:
    int partition(vector<int>& nums, int left, int right){
    int pivot = nums[right];
    for(int i = 0; i < nums.size(); i++){
    if(nums[i]<pivot){
    swap(nums,left++,i);

            }
        }
        swap(nums,left,right);
        return left;
    }
    int findKthLargest(vector<int>& nums, int k) {
        int left = 0, right = nums.size()-1;
        while(true){
            int pos = partition(nums,left,right);
            if(pos == k) return nums[k];
            if(pos > k) right = pos - 1;
            else left = pos + 1;
        }
        
    }
    void swap(vector<int>& nums,int i,int j){
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }
    

    };''


Log in to reply
 

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