Lintcode -第K大元素

class Solution {
public:
#if 0
    /*
     * @param n: An integer
     * @param nums: An array
     * @return: the Kth largest element
     */
    int kthLargestElement(int n, vector &nums) {
        // write your code here
        int len = nums.size();
        priority_queue, greater> que; // build samllest //heap
        for(int i = 0; i < len; i ++) {
            if(que.size() == n) { // 当queue到到达了n的时候
                if(nums[i] > que.top()) {
                    que.pop();
                    que.push(nums[i]);
                }
            }else {
                que.push(nums[i]); // insert it directly
            }
        }
        return que.top();
    }
#endif
#if 0
    /*
     * @param n: An integer
     * @param nums: An array
     * @return: the Kth largest element
     */
    typedef multiset> intSet;
    typedef multiset>::iterator setIterator; 
    int kthLargestElement(int n, vector &nums) {
        // write your code here
        intSet st;
        int len = nums.size();
        for (int i = 0; i < len; i++) {
            if (st.size() == n) {
                setIterator iterGreatest = st.begin();
                if (nums[i] > *st.begin()) {
                    st.erase(iterGreatest);
                    st.insert(nums[i]);
                }
            } else {
                st.insert(nums[i]);
            }
        }
        return *st.begin(); 
    }
#endif
#if 1   
    /*
     * @param n: An integer
     * @param nums: An array
     * @return: the Kth largest element
     */
    int kthLargestElement(int n, vector &nums) {
        // write your code here
        int l = 0;
        int r = nums.size() - 1;
        
        int index = LomutoPartition(nums, l, r);
        //int index = HoarePartition(nums, l, r);
        while (index != n-1) {
            if (index > n-1) {
                r = index - 1;
                index = LomutoPartition(nums, l, r);
                //index = HoarePartition(nums, l, r);
            } else {
                l = index + 1;
                index = LomutoPartition(nums, l, r);
                //index = HoarePartition(nums, l, r);
            }
        }
        
        return nums[index];
    }
    // 第一个元素作为中轴,进行划分  
    int HoarePartition(vector &A, int l, int r) {  
        int p = A[l];//dealPivot(A, l, r);//RandomInRange(A, l, r);  
        int i = l;  
        int j = r+1;  
          
        do {  
            do {  
                i++;  
            } while (A[i] > p);  
              
            do {  
                j--;  
            } while (A[j] < p);  
              
            swap(A[i], A[j]);  
        } while (i < j);  
        swap(A[i], A[j]); //当i>=j撤销最后一次交换  
        swap(A[l], A[j]);  
          
        return j;  
    }
    // 第一个元素作为中轴,进行划分  
    int dealPivot(vector &A, int l, int r) {  
        int mid = (l + r) / 2;  
        if (A[l] < A[mid]) {  
            swap(A[l], A[mid]);  
        }  
        if (A[l] < A[r]) {  
            swap(A[l], A[r]);  
        }  
        if (A[r] > A[mid]) {  
            swap(A[r], A[mid]);  
        }  
        // 放到最左边  
        swap(A[l], A[mid]);  
        return A[l];  
    }
    // l和r中间的随机数作为中轴  
    int RandomInRange(vector &A, int l,int r) {  
        srand ( time(NULL) );  
        int num = rand() % (r-l+1) + l;  
        // 放到最左边  
        swap(A[l], A[num]);  
        return A[l];  
    }
    //
    // 第一个元素作为中轴,进行划分,从左到右进行单项扫描划分  
    int LomutoPartition(vector &A, int l, int r) {  
        int p = A[l];  
        int s = l;  
          
        for (int i=l+1; i<=r; i++) {  
            if (A[i] > p) {  
                s++; // 先增加s的右边  
                swap(A[s], A[i]);// 交换到最右边的s位置  
            }  
        }  
          
        swap(A[l], A[s]);  
        return s;  
    }
#endif
};

你可能感兴趣的:(数据结构与算法)