编程之美上一样的题目。寻找发帖水王。

利用分治的思想。

int majorityElement(int* nums, int numsSize) {
int candidate;
int nTimes,i;
for(i=0,nTimes=0;i<numsSize;i++){
if(nTimes==0){
candidate=nums[i];
nTimes++;
}
else{
if(candidate==nums[i])
nTimes++;
else
nTimes--;
}
}
return candidate;
}

  

Majority Element || leetcode的更多相关文章

  1. 169 Majority Element [LeetCode Java实现]

    题目链接:majority-element /** * Given an array of size n, find the majority element. The majority elemen ...

  2. 169. Majority Element - LeetCode

    Question 169. Majority Element Solution 思路:构造一个map存储每个数字出现的次数,然后遍历map返回出现次数大于数组一半的数字. 还有一种思路是:对这个数组排 ...

  3. [LeetCode] Majority Element II 求众数之二

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...

  4. [LeetCode] Majority Element 求众数

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  5. LeetCode Majority Element I && II

    原题链接在这里:Majority Element I,Majority Element II 对于Majority Element I 来说,有多重解法. Method 1:最容易想到的就是用Hash ...

  6. 【LeetCode 229】Majority Element II

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...

  7. LeetCode 169. Majority Element (众数)

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  8. 【一天一道LeetCode】#169. Majority Element

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  9. leetcode 169 Majority Element 冰山查询

    Given an array of size n, find the majority element. The majority element is the element that appear ...

随机推荐

  1. BZOJ2819 Nim(DFS序)

    题目:单点修改.树链查询. 可以直接用树链剖分做.. 修改是O(QlogN),查询是O(QlogNlogN),Q=N=500000: 听说会超时.. 这题也可以用DFS序来做. 先不看修改,单单查询: ...

  2. POJ2763 Housewife Wind(DFS序)

    题目:单边修改,树链查询. 这题是边权,不是点权,不过也可以看作是点权. 然后其实就和BZOJ2819一样. #include<cstdio> #include<cstring> ...

  3. HDU 1241 (DFS搜索+染色)

    题目链接:  http://acm.hdu.edu.cn/showproblem.php?pid=1241 题目大意:求一张地图里的连通块.注意可以斜着连通. 解题思路: 八个方向dfs一遍,一边df ...

  4. CodeForces 450B (矩阵快速幂模板题+负数取模)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=51919 题目大意:斐波那契数列推导.给定前f1,f2,推出指定第N ...

  5. tyvj100题留念

    全是水题萌萌哒~0~... 留个纪念

  6. 【BZOJ】1303: [CQOI2009]中位数图(特殊的技巧)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1303 依旧是题解流,,,不看题解没法活,,,第一眼就是瞎搞,然后就是暴力,显然TLE..题解啊题解. ...

  7. jQuery.Validate验证库详解

    一.用前必备官方网站:http://bassistance.de/jquery-plugins/jquery-plugin-validation/ API: http://jquery.bassist ...

  8. uva10098 Generating Fast, Sorted Permutation

    #include"iostream"#include"stdio.h"#include"string.h"#include"alg ...

  9. Mesa10.2在Win7上的编译

    Mesa10仍然支持Windwos/linux,但是编译方式已经不提供makefile或者workspace Building on windows requires several open-sou ...

  10. Javascript 笔记与总结(2-1)Javascript 与 DOM

    浏览器有渲染 html 的功能,把 html 源码在内存里形成一个 DOM 对象,就是文档对象. 浏览器内部有一个 js 的解释器 / 执行器 / 引擎,如 Chrome 的 V8 引擎. 在 htm ...