leetcode + 投票法则 求众数

原创 2018年04月16日 12:25:18
点击打开链接
//投票法求众数
class Solution {
public:
    vector<int> majorityElement(vector<int>& nums) {
        int counter1=0, counter2=0;
        int nums1=0, nums2=0;
        for(int i=0; i<nums.size(); i++){
            if(nums1 == nums[i]){
                counter1+=1;
            }
            else if(nums2 == nums[i]){
                counter2 +=1;
            }
            else if(counter1 == 0){
                counter1 +=1;
                nums1 =nums[i];
            }
            else if(counter2==0){
                counter2 +=1;
                nums2 = nums[i];
            }
            else{
                counter1-=1;
                counter2-=1;
            }
        }
        vector<int> res;
        counter1=0, counter2=0;
        for(int i=0; i<nums.size();i++){
            if(nums[i]==nums1) counter1+=1;
            else if(nums[i]== nums2) counter2+=1;
        }
        if(counter1 >nums.size()/3) res.push_back(nums1);
        if(counter2 >nums.size()/3) res.push_back(nums2);
        return res;
    }
};

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013554860/article/details/79959015

LeetCode 169. Majority Element(众数)

原题网址:https://leetcode.com/problems/majority-element/ Given an array of size n, find the majority e...
  • jmspan
  • jmspan
  • 2016-05-25 01:37:08
  • 604

leetcode + 投票法则求众数

点击打开链接class Solution { public: int majorityElement(vector&amp;lt;int&amp;gt;&amp;amp; nums) { ...
  • u013554860
  • u013554860
  • 2018-04-16 12:26:07
  • 1

[Leetcode,python] Majority Element 众数

Given an array of size n, find the majority element. The majority element is the element that appear...
  • ysk0825
  • ysk0825
  • 2016-12-22 00:11:56
  • 144

169. 求众数

169. 求众数 虽然用map记录也能过,但是明显其复杂度高,一个o(n)的解法(貌似是某年408算法题) class Solution { public: int majorityE...
  • gl486546
  • gl486546
  • 2018-04-02 07:37:06
  • 35

LeetCode 229. Majority Element II(众数II)

原题网址:https://leetcode.com/problems/majority-element-ii/ Given an integer array of size n, find all ...
  • jmspan
  • jmspan
  • 2016-04-05 06:28:28
  • 834

LeetCode169详细分析并附求解代码

文章分析解决了LeetCode上第169题,并附上了完整的代码。提供了使用Map方法来进行对数组中不同元素的计数问题,最终了击败了9.65的对手。欢迎大家相互交流学习,共同进步。^_^(ps:如果读者...
  • SEUer_jeff
  • SEUer_jeff
  • 2016-11-07 22:05:50
  • 277

169/229 Majority Element(寻找众数)

169 Majority Element Given an array of size n, find the majority element. The majority eleme...
  • NichChen
  • NichChen
  • 2017-08-07 18:09:51
  • 89

leetcode解题笔记:Majority Element I & II

剑指offer第29题,leetcode 169,229 题目:找到数组中出现次数超过一半的数字 Given an array of size n, find the majority elem...
  • crystal6918
  • crystal6918
  • 2016-11-17 22:18:36
  • 240

[leetcode] 229. Majority Element II

Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algori...
  • TstsUgeg
  • TstsUgeg
  • 2016-04-11 11:36:10
  • 622

【LeetCode】Majority Element 解题报告

Majority Element[LeetCode]https://leetcode.com/problems/majority-element/Total Accepted: 110538 Tota...
  • fuxuemingzhu
  • fuxuemingzhu
  • 2016-05-01 00:02:10
  • 288
收藏助手
不良信息举报
您举报文章:leetcode + 投票法则 求众数
举报原因:
原因补充:

(最多只允许输入30个字)