《剑指offer》39题—数组中出现次数超过一半的数字
题目描述
方法1:
1 class Solution {
public:
int MoreThanHalfNum_Solution(vector<int> numbers) {
int n = numbers.size();
if(n == )
return ;
int begin=, end=n;
int index = partition(numbers, , n);
while(index != n/){
if(index < n/)
begin = index + ;
else
end = index;
index = partition(numbers, begin, end);
}
int cnt = ;
for(int i=; i<n; ++i){
if(numbers[index] == numbers[i])
++cnt;
}
return cnt > numbers.size()/ ? numbers[index] : ;
}
int partition(vector<int> arr, int begin, int end){
int pivot = arr[begin];
while(begin < end){
while(begin < end && arr[--end] >= pivot);
arr[begin] = arr[end];
while(begin < end && arr[++begin] <= pivot);
arr[end] = arr[begin];
}
arr[begin] == pivot;
return begin;
}
};
方法2:
class Solution {
public:
int MoreThanHalfNum_Solution(vector<int> numbers) {
if(numbers.size() == )
return ;
int majority = numbers[], cnt = ;
for(int i=; i<numbers.size(); ++i){
if(cnt == ){
majority = numbers[i];
cnt = ;
}
else
cnt = numbers[i]==majority ? cnt+ : cnt-;
}
cnt = ;
for(int i=; i<numbers.size(); ++i){
if(majority == numbers[i])
++cnt;
}
return cnt > numbers.size()/ ? majority : ;
}
};
《剑指offer》39题—数组中出现次数超过一半的数字的更多相关文章
- 【剑指offer】73.数组中出现次数超过一半的数字
73.数组中出现次数超过一半的数字 知识点:数组:哈希:占领地思想: 题目描述 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组{1,2,3,2,2,2,5,4 ...
- 【Offer】[39] 【数组中出现次数超过一半的数字】
题目描述 思路分析 测试用例 Java代码 代码链接 题目描述 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如,输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于数 ...
- 剑指offer-面试题39-数组中出现次数超过一半的数字-抵消法
/* 题目: 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字. 例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于数字2在数组中出现了5次,超过数组长度的一半,因此输 ...
- 剑指offer系列54---数组中出现次数超过一半的数
[题目]数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字. * 例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}. * 由于数字2在数组中出现了5次,超过数组长度的一半,因 ...
- 剑指offer-面试题39-数组中出现次数超过一半的数字-快速排序
/* 题目: 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字. 例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于数字2在数组中出现了5次,超过数组长度的一半,因此输 ...
- 《剑指offer》第三十九题(数组中出现次数超过一半的数字)
// 面试题39:数组中出现次数超过一半的数字 // 题目:数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例 // 如输入一个长度为9的数组{1, 2, 3, 2, 2, 2, 5, ...
- Leetcode - 剑指offer 面试题29:数组中出现次数超过一半的数字及其变形(腾讯2015秋招 编程题4)
剑指offer 面试题29:数组中出现次数超过一半的数字 提交网址: http://www.nowcoder.com/practice/e8a1b01a2df14cb2b228b30ee6a92163 ...
- 剑指Offer:数组中出现次数超过一半的数字【39】
剑指Offer:数组中出现次数超过一半的数字[39] 题目描述 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如,输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于这 ...
- 剑指 Offer 39. 数组中出现次数超过一半的数字 + 摩尔投票法
剑指 Offer 39. 数组中出现次数超过一半的数字 Offer_39 题目描述 方法一:使用map存储数字出现的次数 public class Offer_39 { public int majo ...
随机推荐
- hdu1693Eat the Trees(插头dp)
传送门 先坑着,等啥时候会了再来填坑 不得不说思路真的是很妙啊 //minamoto #include<iostream> #include<cstdio> #include& ...
- JS中的MOD运算
最近研究汉诺塔非递归的时候,看到书上写了个MOD,久违啊,感觉好久没看到过了,都忘了怎么用了. 某人:我知道,这不就是取余嘛,直接%就行了. 嗯......,如果是python语言,你说的很对,但是我 ...
- 服务器宕机,mysql无法启动,job for mysql.service failed because the process exited with error code,数据库备份与恢复
[问题现象] 服务器在运行过程中,因人为意外导致电源被拔,服务器宕机,mysql重启不成功,报错如下 根据提示,输入systemctl status mysql.service和journalctl ...
- 微信小程序实战
为了积攒粉丝,公司决定做一个一分钱姓名测算的小程序引导大家关注公众号. 实现的需求就是 1 首页 用户编辑姓名和性别进行提交 2 测算结果页 实现分享和支付功能 3 测算历史页面 看到用户曾经测算记 ...
- 15.更新和删除数据--SQL
一.更新数据 更新(修改)表中的数据,可以使用UPDA TE语句.有两种使用UPDA TE的方式: 更新表中的特定行: 更新表中的所有行. 警告:不要省略WHERE子句 在使用UPDA TE时一定要细 ...
- Django模板语言,过滤器整理
Django模板语言,过滤器整理 1. add {{ value|add:"2" }} 把add后的参数加给value: 处理时,过滤器首先会强制把两个值转换成Int类型. 如果强 ...
- urllib2基础操作
Urllib2基础操作 1.打开网页(urlopen) 打开一个网页 import urllib2 response = urllib2.urlopen('http://www.baidu.com') ...
- js 创建方法
贴个代码先: function O(user,pwd){ //use constructor this.user=user; this.pwd=pwd; this.get=get; return th ...
- Codeforces 163E(ac自动机、树状数组)
要点 显然ac自动机的板子就可以暴力一下答案了 为了优化时间复杂度,考虑套路fail树的dfs序.发现本题需要当前这个尾点加上所有祖先点的个数,考虑使用树状数组差分一下,在父点+1,在子树后-1,每次 ...
- Ionic开发-搭建开发环境
1安装node.js 2安装ionic & cordova: 命令行输入:npm install –g cordova ionic 注:-g表示全局安装,也可以进入指定的目录安装,但这里推荐全 ...