More than half of the array of numbers

[Problem] array has the number of times a number appears more than half the length of the array, find this number. For example, a length of the input array 9 {1,2,3,2,2,2,5,4,2}. Since the number 2 appears five times, more than half the length of the array in the array, the output 2. If there is 0 output.

[Thinking] First, the first idea, we do not consider the complexity of the space, which is best used when the written test, using a hash table and then iterate, due unordered_map not allowed duplicate Key , and therefore each traversed the same key, value is incremented. Then determine the value is greater no more than half of the array, if greater than return directly to, otherwise it returns zero.

class Solution {
public:
    int MoreThanHalfNum_Solution(vector<int> numbers) {
        unordered_map<int, int> hash_map;
        if(numbers.size() == 1) return 1;
        for(auto i: numbers){
            hash_map[i]++;
            if(hash_map[i] > (numbers.size() / 2)){
                return i;
            }
        }
        return 0;
    }
};

When another idea, to perform the entire sorting operation sequence, if the number exceeds a half of the number in the array of the array, the entire array after sorting, this number must in an intermediate position of the array! Well, since you want to sort, we're using fast row to sort the array, O (Nlogn), although some can be optimized to O (n), but the code is much too complicated! After ordering, we first obtain the value of the intermediate position, and then traverse the entire array is sorted, count the number of this value, if indeed larger than the size / 2, the number of the number of returns! Since you want to learn algorithms, try not to cold storage, and honestly write their own fast row!

class Solution {
public:
    vector<int> qs_partition(vector<int>& list, int L, int R){
        int more = R;   // 选取最后一个数为分界点
        int less = L - 1;
        int cur = L;
        vector<int> res(2);
        while(cur < more){
            if(list[cur] < list[R]){
                swap(list[++less], list[cur++]);
            }the else  IF (List [cur]> List [R & lt]) { 
                the swap (List [ - More], List [cur]);  
                 // after switching cur may be smaller than an intermediate value, but also need to be exchanged again, and therefore is not self-cur add 
            } the else { 
                CUR ++ ; 
            } 
        } 
        the swap (List [More], List [R & lt]); 
        RES [ 0 ] + = less . 1 ; 
        RES [ . 1 ] = More;
         return RES; 
    } 
    // classic quick drain 
    void the QuickSort ( Vector < int > List &, int L,int R){
        if(list.size() < 2) return;
        if(L < R){
            vector<int> p = qs_partition(list, L, R);
            QuickSort(list, L, p[0]-1);
            QuickSort(list, p[1]+1, R); // 递归处理其他部分
        }
    }

    int MoreThanHalfNum_Solution(vector<int> numbers) {
        if(numbers.size() == 0) return 0;
         Int size = () numbers.size; 
        the QuickSort (Numbers, 0 , size- . 1 );
         int MID size = / 2 ;    // value obtained intermediate position 
        int COUNT = 0 ;
         for (Auto NUM: Numbers) {
             IF ( == NUM Numbers [mID]) { 
                COUNT ++;      // number of intermediate positions statistical value 
                IF (COUNT> (size / 2 )) {
                     return NUM; 
                } 
            } 
        } 
        return  0;
    }
};

 

Guess you like

Origin www.cnblogs.com/zhudingtop/p/11349737.html