28-數組中出現次數超過一半的數字

一、題目描述:

數組中有一個數字出現的次數超過數組長度的一半,請找出這個數字。例如輸入一個長度爲9的數組{1,2,3,2,2,2,5,4,2}。由於數字2在數組中出現了5次,超過數組長度的一半,因此輸出2。如果不存在則輸出0。

二、思路分析:

1、先後兩次遍歷數組,第一次遍歷數組並用map統計數組中每個數字出現的次數;第二次遍歷數組找出出現次數超過數組長度一半的數字。

C++:

class Solution {
public:
    int MoreThanHalfNum_Solution(vector<int> numbers) {
        int length = numbers.size();
        if(length == 0){
            return 0;
        }
        //遍歷數組,統計每個數字出現的次數
        map<int,int> numMap;
        for(int i=0; i<length; i++){
            numMap[numbers[i]]++;
        }
        //遍歷數組,找出第一個出現次數超過數組長度一半的數字
        for(int i=0; i<length; i++){
            if(numMap[numbers[i]] > length/2){
                return numbers[i];
            }
        }
        return 0;
    
    }
};

python:

# -*- coding:utf-8 -*-
class Solution:
    def MoreThanHalfNum_Solution(self, nums):
        # write code here
        length = len(nums)
        if length == 0:
            return 0
        numMap = {}
        for num in nums:
            if  num not in numMap:
                numMap[num] = 1
            else:
                numMap[num] += 1
        for num in nums:
            if numMap[num] > length//2:
                return num
        return 0

 

2、思路:https://www.bilibili.com/video/BV187411W7A9?from=search&seid=6211879220294896728

在上述視頻中博主介紹了四種方法,這裏實現的是第四種。

方法:第一次遍歷數組,找到數組中出現次數最多的數字result;第二次遍歷數組,判斷數字result出現的次數是否超過數組長度的一半。

第一次遍歷數組的遍歷規則:初始化result爲數組的第一個元素,並初始化其出現次數times爲1。如果下一個數字和result相同,則result對應的次數加1;如果不同,次數減1。如果次數times減爲零,將下一個數字初始化爲result,並初始化該數字出現次數爲1。由於我們要找的數字出現的次數比其他所有數字出現的次數之和還要多,那麼遍歷完數組後得到的result就是出現次數最多的元素。再判斷出現次數是否超過數組長度的一半。

C++實現:

class Solution {
public:
    int MoreThanHalfNum_Solution(vector<int> numbers) {
        
        int result = numbers[0];//從第一個元素開始,初始化出現的次數爲1
        int times = 1;
        for(int i = 1; i < numbers.size(); ++i){//遍歷數組中的每個元素,若與前一個元素相同,則則該元素出現的次數加1,否則減1,當次數爲0的時候初始化下一個元素出現的次數爲1
            if(times == 0){
                // 更新result的值爲當前元素,並置次數爲1
                result = numbers[i];
                times = 1;
            }
            else if(numbers[i] == result){
                times++;
            }
            else{
                times--;
            }
        }
        //經過上面的for循環,times對應的元素result就是數組中出現次數最多的一個元素,但具體出現的次數並不知道
        //下面判斷次這個元素出現的次數是不是超過數組長度的一半
        times = 0;
        for(int i=0; i<numbers.size(); i++){
            if(result == numbers[i]){
                times++;
            }
        }
         return (times > (numbers.size() >> 1)) ? result : 0;//右移一位相當於除2,左移乘2
    
    }
};

python實現:

# -*- coding:utf-8 -*-
class Solution:
    def MoreThanHalfNum_Solution(self, numbers):
        result = numbers[0]
        times = 1
        for i in range(1,len(numbers)):
            if times == 0:
                result = numbers[i]
                times = 1
            elif numbers[i]==result:
                times += 1
            else:
                times -= 1
                
        times = 0
        for val in numbers:
            if val==result:
                times += 1
        return result if times>len(numbers)//2 else 0

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章