【数学知识】剑指 Offer 39. 数组中出现次数超过一半的数字

本问题对应课程链接:剑指offer刷题第三期

要求:
1、请打卡你的解题代码,如果能简单说一下思路则简单说一下思路。
2、给出你代码所用的时间复杂度和空间复杂度

共以下 25 个回答

  • 九号球 普通 2023年11月23日 下午11:03

    class Solution {
    public int inventoryManagement(int[] stock) {
    //不同就抵消,最后剩余的数的数量肯定大于半数
    int vote = 0, x = 0;
    for (int num : stock) {
    if (vote 0) {
    x = num;
    }
    vote = x
    num ? vote + 1: vote – 1;
    }
    return x;
    }
    }

    0 赞同 0 条回复
  • 招舟子 普通 2023年9月7日 下午5:46

    class Solution {
    public int majorityElement(int[] nums) {
    int res = 0;
    HashMap<Integer,Integer> hashmap = new HashMap<>();
    for(int i=0;i<nums.length;i++){
    if(hashmap.containsKey(nums[i])){
    hashmap.replace(nums[i],hashmap.get(nums[i])+1);
    }else{
    hashmap.put(nums[i],1);
    }
    }
    for(Integer i:hashmap.keySet()){
    if(hashmap.get(i)>(nums.length/2)){
    return i;
    }
    }
    return 0;
    }
    }
    直接hashmap

    0 赞同 0 条回复
  • jhfuture 普通 2023年8月26日 下午10:25

    “`java
    class Solution {
    public int majorityElement(int[] nums) {
    if(nums.length<=2) return nums[0];

            // 投票法,将第一个认定为众数,投票数为1,然后遍历,碰到不同则票数-1。然后遍历下一个
            int result=nums[0];
            int vote=0;
            for(int i=0;i<nums.length;i++){
                if(vote==0){
                    result=nums[i];
                    vote=1;
                }else{
                    if(nums[i]==result){
                        vote++;
                    }else{
                        vote--;
                    }
                }
            }
            return result;
    }
    

    }

    0 赞同 0 条回复
  • 彦,,# 普通 2023年8月5日 下午4:04
    时间复杂度是O(nlogn), 空间复杂度是O(logn)
    nt majorityElement(vector<int>& nums) {
            unordered_map<int ,int> counts;
            int res =0,cnt = 0;
            for (int num :nums)
            {
                ++counts[num];
                if(counts[num] >cnt)
                {
                    res = num;
                    cnt = counts[num];
                }
            }
        return res;
        }
    
    0 赞同 0 条回复
  • mpweixin用户 普通 2023年8月2日 上午9:13
    java
    class Solution {
        public int majorityElement(int[] nums) {
            int votes = 0;
            int x = 0;
            for(int num : nums) {
                if(votes == 0) {
                    x = num;
                }
                votes += (x == num ? 1 : -1);
            }
    
            return x;
        }
    }
    

    投票法:时间复杂度是O(N), 空间复杂度是O(1)

    0 赞同 0 条回复
  • 屠龙手段 普通 2023年7月23日 下午9:12

    投票法,时间复杂度O(n) 空间复杂度O(1)

    class Solution:
        def majorityElement(self, nums: List[int]) -> int:
            res, cnt = None, 0
            for num in nums:
                res = res if cnt else num
                cnt += (1 if res == num else -1)
            return res
    
    0 赞同 0 条回复
  • 憨憨 永久会员 2023年7月20日 下午12:08

    投票法
    时间O(n)
    空间O(1)

    class Solution {
        public int majorityElement(int[] nums) {
            if(nums.length <= 2)return nums[0];
    
            int x = nums[0];
            int sum = 1;
    
            for(int i = 1; i < nums.length; i++){
                if(sum == 0){
                    x = nums[i];
                    sum = 1;
                }else{
                    if(nums[i] == x){
                        sum ++;
                    }else{
                        sum --;
                    }
                }
            }
            return x;
        }
    }
    
    0 赞同 0 条回复
  • 怀瑾 永久会员 2023年7月19日 下午3:33

    C++实现

    “`C++
    int majorityElement(vector& nums) {
    int count = 0;
    int candidate = 0;

    for (int num : nums) {
        if (count == 0)
            candidate = num;
    
        if (num == candidate)
            count++;
        else
            count--;
    }
    
    return candidate;
    

    }

    “`

    0 赞同 0 条回复
  • 名字什么的好麻烦 普通 2023年7月16日 下午9:49

    哈希

    class Solution {
        Map<Integer,Integer> map = new HashMap<>();
        public int majorityElement(int[] nums) {
            count(nums);
            Map.Entry<Integer,Integer> major = null;
            for(Map.Entry<Integer,Integer> entry : map.entrySet()){
                if(major==null||entry.getValue()>major.getValue())
                    major=entry;
            }
            return major.getKey();
        }
        public void count(int[] nums){
            int l=nums.length;
            for(int i=0;i<l;i++)
                map.put(nums[i],map.getOrDefault(nums[i],0)+1);
        }
    }
    
    0 赞同 0 条回复
  • 江畅 普通 2023年7月16日 下午1:36
    // 数组中出现次数超过一半的数字
    // 时间复杂度 O(N)
    // 空间复杂度 O(1)
    class Solution {
    public:
        int majorityElement(vector<int>& nums) {
            int res = 0;
            int count = 0;
    
            for (int i = 0; i < nums.size(); ++i) {
                if (count == 0) { res = nums[i]; count = 1; }
                else if (nums[i] == res) { ++count; }
                else { --count; }
            }
    
            return res;
        }
    };
    
    0 赞同 0 条回复
  • 哈哈哈哈开开心心 普通 2023年7月16日 上午4:57
    class Solution:
        def majorityElement(self, nums: List[int]) -> int:
            n = len(nums)
    
            if n <= 2:
                return nums[0]
    
            cnt = 1
            cur = nums[0]
    
            for i in range(1, n):
                if cnt == 0:
                    cur = nums[i]
                    cnt = 1
                else:
                    if nums[i] == cur:
                        cnt += 1
                    else:
                        cnt -= 1
    
            return cur
    

    摩尔

    0 赞同 0 条回复
  • BOZ 普通 2023年7月15日 下午5:38

    【思路】对于一个majority数,将其与其他数字一比一抵消后依然会有多余,利用这个性质遍历一遍数组即可
    【复杂度】时间复杂度O(n),空间复杂度O(1)

    class Solution {
    public:
        int majorityElement(vector<int>& nums) {
            int majority = nums[0], cnt = 0;
            for (auto &n: nums) {
                if (n == majority) cnt++;
                else if (cnt) cnt--;
                else {majority = n; cnt = 1;}
            }
            return majority;
        }
    };
    
    0 赞同 0 条回复
  • 花老爷 普通 2023年7月14日 下午10:44

    排序
    时间:O(nlogn)
    空间:O(logn)

    class Solution {
        public int majorityElement(int[] nums) {
            Arrays.sort(nums);
            int count = 1;
            for (int i = 1; i < nums.length; i++) {
                if (nums[i] == nums[i-1]) {
                    count++;
                    if (count > nums.length / 2) {
                        return nums[i];
                    }
                } else {
                    count = 1;
                }
            }
            return nums[0];
        }
    }
    

    官解 Boyer-Moore 投票算法
    时间:O(n)
    空间:O(1)

    class Solution {
        public int majorityElement(int[] nums) {
            int count = 0;
            Integer candidate = null;
    
            for (int num : nums) {
                if (count == 0) {
                    candidate = num;
                }
                count += (num == candidate) ? 1 : -1;
            }
    
            return candidate;
        }
    }
    
    0 赞同 0 条回复
  • Ming 普通 2023年7月13日 下午2:42
    class Solution {
        public int majorityElement(int[] nums) {
            Arrays.sort(nums);
            return nums[nums.length/2];
        }
    }
    

    肯定在中间

    0 赞同 0 条回复
  • Dsjh 普通 2023年7月11日 上午11:24

    MY

    摩尔投票

    class Solution {
    public:
        int majorityElement(vector<int>& nums) {
            int ans = 0;
            int num = 0;
            for(int i=0;i<nums.size();i++){
                if(num == 0){
                    ans = nums[i];
                    num++;
                    continue;
                }
                if(nums[i]!=ans){
                    num--;
                }
                else{
                    num++;
                }
            }
            return ans;
        }
    };
    
    0 赞同 0 条回复
  • Vow the vow。 普通 2023年7月11日 上午10:57

    摩尔投票法,学到了

    public int majorityElement(int[] nums) {
            int x = -1, votes = 0;
            for(int num : nums){
                if(votes == 0){
                    x = num;
                }
                votes += (num == x) ? 1 : -1;
            }
            return x;
        }
    
    0 赞同 0 条回复
  • 局外人 普通 2023年7月11日 上午9:26

    思想:采用哈希表记录次数
    时间复杂度O(n) 空间复杂度O(n)

    class Solution {
        public int majorityElement(int[] nums) {
            int n = nums.length;
            int half = n/2;
    
            HashMap<Integer, Integer> map = new HashMap<>();
    
            int res = 0;
            for(int x:nums){
                if(map.get(x)!=null){
                    int value = map.get(x);
                    map.replace(x, value+1);
                }else{
                    map.put(x, 1);
                }
    
                if(map.get(x)!=null && map.get(x)>half){
                    res = x;
                    break;
                } 
            }
            return res;
        }
    }
    
    0 赞同 0 条回复
  • 行动派小彩笔 普通 2023年7月10日 下午11:33
    // 时间复杂度O(n),空间复杂度O(1)
    class Solution {
    public:
        int majorityElement(vector<int>& nums) {
            if(nums.size()==0) return -1;
            int cond = nums[0];
            int count = 1;
            for(auto num:nums)
            {
                if(num == cond) count++;
                else
                {
                    if(--count==0)
                    {
                        cond = num;
                        count = 1;
                    }
                }
            }
            return cond;
        }
    };
    
    0 赞同 0 条回复
  • mpweixin用户 永久会员 2023年7月10日 下午10:48
    //时间O(n) 空间O(1)
    public int majorityElement(int[] nums) {
            int candidate = nums[0];
            int count = 1;
            for(int i = 1;i < nums.length;i++){
                if(count == 0){
                    candidate = nums[i];
                    count = 1;
                }else{
                    if(nums[i] == candidate){
                        count++;
                    }else{
                        count--;
                    }
                }
    
            }
            return candidate;
        }
    
    0 赞同 0 条回复
  • Jerry 普通 2023年7月10日 下午10:44
     // 摩尔投票法:粒子带电性
    class Solution {
        public int majorityElement(int[] nums) {
            int target = 0; //寻找的数
            int vote = 0; //出现次数
            for (int num : nums) {
                if (vote == 0) { //出现次数为0时,假设 num就时 target
                    target = num;
                    vote = 1;
                } else if (num == target) {
                    vote++; // 如果遇到众数,则vote+1
                } else {
                    vote--; // 如果遇到不是,则vote-1
                }
            }
            return target; //最后返回的target,必然是众数
        }
    }
    

    时间复杂度:O(n)
    空间复杂度:O(1)

    0 赞同 0 条回复
  • 张君雅小妹妹 普通 2023年7月10日 下午10:04

    时间复杂度:O(N)
    空间复杂度:O(1)

    class Solution {
        public int majorityElement(int[] nums) {
            // 摩尔投票法
            if(nums.length <= 2) return nums[0];
            int x = nums[0], sum = 1;
            for(int i = 1; i < nums.length; i++){
                if(sum == 0){
                    x = nums[i];
                    sum = 1;
                } else {
                    if(x == nums[i]){
                        sum++;
                    } else {
                        sum--;
                    }
                }
            }
            return x;
    
        }
    }
    
    0 赞同 0 条回复
  • yxy 普通 2023年7月10日 下午7:00

    时间复杂度O(n)

    class Solution {
    public:
        int majorityElement(vector<int>& nums) {
            int count = 1;
            int num = nums[0];
            for(int i=1;i<nums.size();i++)
            {
                if(nums[i]==num)
                    count++;
                else
                {
                    count--;
                    if(count==0)
                    {
                        count = 1;
                        num = nums[i];
                    }
                }
            }
            return num;
        }
    };
    
    0 赞同 0 条回复
  • 小吕 普通 2023年7月10日 下午4:33

    c++实现
    int majorityElement(vector& nums) {
    int candidate =-1;
    int count = 0;
    for(int num:nums)
    {
    if(numcandidate) count++;
    else if(–count<0)
    {
    candidate = num;
    count=1;
    }
    }
    return candidate;
    }

    0 赞同 0 条回复
  • 张开怀 普通 2023年7月10日 下午3:15
    class Solution{
    public:
           int majorityElement(vector<int>& nums){
                  if (num.size() <= 2){
                         return nums[0];
                  }
    
                  int x = nums[0];
                  int sum =1;
                  for(int i=1; i<num.size(); i++){
                         if(sum == 0){
                                x = nums[i];
                                sum =1;
                         }else{
                                if(x == nums[i]){
                                       sum++;
                                }else{
                                       sum--;
                                }
                         }
                  }
                  return x;
           }
    };
    

    时间复杂度:O(n)
    额外空间复杂度:O(1)

    0 赞同 0 条回复
  • Yodl. 普通 2023年7月10日 下午2:58
    //法一:哈希表
    //时间复杂度O(n),空间复杂度O(n)
    class Solution {
    public:
        int majorityElement(vector<int>& nums) {
            map<int,int> v;
            int i,ans;
            for(i=0;i<nums.size();i++)
            {
                if(v.count(nums[i])==0)
                {
                    v.insert(make_pair(nums[i], 1));
                }
                else
                {
                    v[nums[i]]++;
                    if(v[nums[i]]>(nums.size()/2))
                    {
                        ans=nums[i];
                    }
                }
            }
            if(nums.size()==1)
            {
                return nums[0];
            }
            return ans;
        }
    };
    
    //法二:摩尔投票法
    //也可以理解成混战极限一换一,不同的两者一旦遇见就同归于尽,最后活下来的值都是相同的,即要求的结果
    //时间复杂度O(n),空间复杂度O(1)
     class Solution {
     public:
         int majorityElement(vector<int>& nums) {
             int res = 0, count = 0;
             for(int i = 0; i < nums.size(); i++){
                 if(count == 0){
                     res = nums[i];
                     count++;
                 }
                 else
                     res==nums[i] ? count++:count--;
             }
             return res;
         }
     };
    
    0 赞同 0 条回复

# 回答此问题

后才能回答