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

利用分治的思想。

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. 贪心 POJ 2586 Y2K Accounting Bug

    题目地址:http://poj.org/problem?id=2586 /* 题意:某公司要统计全年盈利状况,对于每一个月来说,如果盈利则盈利S,如果亏空则亏空D. 公司每五个月进行一次统计,全年共统 ...

  2. 关于配置文件权衡,.config VS .xml

    众所周知,程序的灵活性有一部分就是“配”出来了. 当然,config文件从来就没有让.NET的同学轻松过,至少,我觉得很麻烦. 1.config .NET的配置文件方便,其实最方便的是appSetti ...

  3. 正则表达式学习日记zz

    1."."为通配符,表示任何一个字符,例如:"a.c"可以匹配"anc"."abc"."acc": ...

  4. 一人一python挑战题解

    题目id: 1 just print a+b give you two var a and b, print the value of a+b, just do it!! print a+b 题目id ...

  5. COJ 删除数字

    试题描述 输入正整数N和M,在N中删除掉M位,能留下的最大整数是多少? 输入 正整数N和M 输出 留下的最大整数 输入示例 233390323 5 输出示例 9323 其他说明 1<=N< ...

  6. poi读写word模板 / java生成word文档

    有一word文档表格 形如: 姓名 ${name} 电话 ${tel} 从数据库读取记录替换上述变量 import java.io.FileOutputStream; import java.util ...

  7. CentOS网卡配置文件

    [root@xaiofan ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0TYPE=EthernetONBOOT=yesNM ...

  8. 每用户订阅上的所有人SID 不存在

    ArcEngine开发查询时出现异常 摘自:http://shaopengluo.blog.163.com/blog/static/1314464152011112144855776/ 检查发现是Qu ...

  9. U-Mail邮件服务系统任意文件上传+执行漏洞(runtime缺陷与验证绕过)

    http://www.wooyun.org/bugs/wooyun-2010-061859

  10. LeetCode | Regular Expression Matching

    Regular Expression Matching Implement regular expression matching with support for '.' and '*'. '.' ...