剑指offer——数组中出现次数超过一半的数字(c++)
题目描述
数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}。由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2。如果不存在则输出0。
思路一
遍历数组是保存两个值:一个是数字中的一个数字,另一个是次数。当遍历到下一个数字的时候,如果下一个数字和之前保存的数字相等,则次数加1;如果不同,则次数减1;如果次数为零,那么我们需要保存下一个数字,并把次数设置为1。
由于我们要找的数字出现的次数比其他所有数字出现的次数之和还要多,那么要找的数字肯定是最后一次把次数设置为1时对应的数字。
class Solution {
public:
int MoreThanHalfNum_Solution(vector<int> nums) {
if(nums.size() <= 0){
return 0;
}
int res = nums[0];
int times = 1;
for(int i = 1; i < nums.size(); ++i){
if(times == 0){
res = nums[i];
times = 1;
}
else if(nums[i] == res){
times++;
}
else{
times--;
}
}
if(!CheckMoreHalf(nums, res)){
res = 0;
}
return res;
}
private:
//判断这个数字是否出现次数超过一半
bool CheckMoreHalf(vector<int>& nums, int result){
int times = 0;
for(int i = 0; i < nums.size(); ++i){
if(nums[i] == result){
++times;
}
}
if(times * 2 <= nums.size()){
return false;
}
return true;
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
思路二
基于快排的思想。将数组排序后,中间的数字即为要寻找的数字,那么,这个数据就是统计学上的中位数,即长度为n的数组中第n/2大的数字。
在随机快排中,随机选择一个数字,调整数组顺序,如果其下标刚好是n/2,那么就找到了中位数;如果其下标小于n/2,则中位数应该在它的右边;如果其下标大于n/2,则中位数应该在它的左边。
class Solution {
public:
int MoreThanHalfNum_Solution(vector<int> nums) {
int n = nums.size();
if(n <= 0){
return 0;
}
int left = 0, right = n - 1;
int mid = n / 2;
int index = Partition(nums, left, right);
while(index != mid){
if(index > mid){
right = index - 1;
index = Partition(nums, left, right);
}
else{
left = index + 1;
index = Partition(nums, left, right);
}
}
int res = nums[mid];
if(!CheckMoreHalf(nums, res)){
res = 0;
}
return res;
}
private:
int Partition(vector<int>& nums, int left, int right){
int pivot = nums[right];
int tail = left - 1;
for(int i = left; i < right; ++i){
if(nums[left] <= pivot){
++tail;
if(tail != i)
swap(nums[tail], nums[i]);
}
}
swap(nums[tail+1], nums[right]);
return tail+1;
}
bool CheckMoreHalf(vector<int>& nums, int result){
int times = 0;
for(int i = 0; i < nums.size(); ++i){
if(nums[i] == result){
++times;
}
}
if(times * 2 <= nums.size()){
return false;
}
return true;
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
---------------------
剑指offer——数组中出现次数超过一半的数字(c++)的更多相关文章
- python剑指offer数组中出现次数超过一半的数字
题目描述 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2. ...
- 剑指Offer——数组中出现次数超过一半的数字——一题多解
看题目: 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2. ...
- 剑指Offer——数组中出现次数超过一半的数字
题目描述: 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2 ...
- 用js刷剑指offer(数组中出现次数超过一半的数字)
题目描述 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2. ...
- 剑指Offer-28.数组中出现次数超过一半的数字(C++/Java)
题目: 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2.如 ...
- 剑指offer--11.数组中出现次数超过一半的数字
unique(), count()函数好用 ---------------------------------------------------------------------- 时间限制:1秒 ...
- 1-剑指offer: 数组中出现次数超过一半的数字
题目描述 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2. ...
- 剑指offer-数组中出现次数超过一半的数字
题目描述 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2. ...
- Leetcode - 剑指offer 面试题29:数组中出现次数超过一半的数字及其变形(腾讯2015秋招 编程题4)
剑指offer 面试题29:数组中出现次数超过一半的数字 提交网址: http://www.nowcoder.com/practice/e8a1b01a2df14cb2b228b30ee6a92163 ...
随机推荐
- Vue - 前端本地项目开发过程中webpack打包内存泄漏问题解决方法
编译项目出现如下错误: FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory 原因: n ...
- Mysql忘记密码:关于ERROR 1045 (28000): Access denied for user 'ODBC'@'localhost' (using password: NO)的问题
命令行登录mysql时,出现ERROR 1045 (28000): Access denied for user 'ODBC'@'localhost' (using password: NO)的提示. ...
- 分布式系统理论基础2 :CAP
本文转自:https://www.cnblogs.com/bangerlee/p/5328888.html 本系列文章将整理到我在GitHub上的<Java面试指南>仓库,更多精彩内容请到 ...
- 为什么要用getBaseContext()方法代替this?(转)
问:this 常常引用当前的 context.但是有些时候,必须使用getBaseContext()来代替this.就是说使用this会引发错误. 如下面的例子: Spinner spinner = ...
- Ruby 技能图谱
# Ruby 技能图谱 说明: 本图谱只捡重点的列举,并非包含全部.文中所列举或没有列举的资源信息都可以在[awesome-ruby](https://github.com/markets/aweso ...
- MySQL导入导出数据和表结构 source和mysqldump
MySQL导入数据的方式: 1.使用source /dir/test.sql导入数据进入数据库:查询数据库编码格式show variables like "%char%";设置编码 ...
- PHP-文件和目录操作
目录操作 创建目录:mkdir(目录地址, 权限, 是否递归创建 = false); 删除目录:rmdir(目录地址);(仅仅可以删除空目录,不支持递归删除) 移动(改名):rename(旧地址, 新 ...
- 安卓真机或者模拟器运行安装应用时提示 Failure [INSTALL_FAILED_NO_MATCHING_ABIS: Failed to extract native libraries, res=-113]解决办法
有时候为了方便调试APP,会在电脑上开启模拟器来调试我们的代码,有时候会出现 Failure [INSTALL_FAILED_NO_MATCHING_ABIS: Failed to extract n ...
- cannot find module node-sass
解决方法: npm install --save-dev node-sass
- leetcode python两整数之和
# Leetcode 371 两整数之和***### 题目描述 **不使用**运算符 `+` 和 `-` ,计算两整数 `a `.`b` 之和. **示例1: ...