剑指offer28:找出数组中超过一半的数字。
1 题目描述
2 思路和方法
(1) 哈希表
用哈希表记录每个元素出现的次数,如果该元素出现次数超过一半,返回1,时间复杂度O(n),空间复杂度O(n)。m[numbers[i]]+=1; map<int, int> m;
(2) 排序
先排序,取中间的数,若这个数在数组中出现超过长度一半,则存在;否则不存在时间复杂度O(nlogn)排序;空间复杂度O(1).
(3) 查找中位数 O(n)
基于partiton函数 O(n),如果存在:该数出现次数超过一半,排序第2/n个元素是该元素;即为中位数
3 C++核心代码
(1)哈希表 m[numbers[i]]+=1; map<int, int> m;
class Solution {
public:
int MoreThanHalfNum_Solution(vector<int> numbers) {
// 哈希表存储某个数出现的次数 hash查询时间复杂度O(1);总时间复杂度O(n)
map<int, int> m;
for (int i = ; i < numbers.size(); ++i) {
m[numbers[i]]+=;
if(m[numbers[i]]>numbers.size()/)
return numbers[i];
}
return ;
}
};
(2) 排序
class Solution {
public:
int MoreThanHalfNum_Solution(vector<int> numbers) {
sort(numbers.begin(),numbers.end());
int key = numbers[numbers.size()/];
int count = ;
for (int i = ; i < numbers.size(); ++i) {
if(key == numbers[i])
++ count;
}
if (count>numbers.size()/)
return key;
return ;
}
};
(3) 查找中位数 O(n)——完整代码
#include <iostream>
int Partition(int A[], int low, int high)
{
int pivot = A[low];
while (low <high)
{
while (low<high && A[high] >= pivot) --high;
A[low] = A[high];
while (low<high && A[low] <= pivot) ++low;
A[high] = A[low];
}
A[low] = pivot;
return low;
}
int HalfData(int a[], int len)
{
int start = ;
int end = len - ;
int middle = len >> ;
int index = Partition(a, start, end); while (index != middle)
{
if (index > middle)
{
end = index - ;
index = Partition(a, start, end);
}
else
{
start = index + ;
index = Partition(a, start, end);
}
}
return a[index];
}
int main()
{
int a[] = { , , , , , , , , };
int len = ;
int result = HalfData(a, );
printf("result:%d\n", result); system("pause");
return ;
}
4 C++完整代码
#include <iostream>
#include <vector> using namespace std; int MoreThanHalfNum(vector<int> numbers)
{
if (numbers.size() == )
{
return ;
} int target = numbers[];
unsigned int cnt = ; for (unsigned int i = ; i < numbers.size(); ++i)
{
if (target == numbers[i])
{
cnt++;
}
else
{
cnt--;
} if (cnt == )
{
cnt = ;
target = numbers[i];
}
}
cnt = ;
for (unsigned int i = ; i < numbers.size(); ++i)
{
if (target == numbers[i])
{
cnt++;
}
} if (cnt * > numbers.size())
{
return target;
} return ;
} int main(void)
{
int a[] = {,,,,,,,,};
vector<int> v(a, a + ); cout<<MoreThanHalfNum(v)<<endl; return ;
}
https://blog.csdn.net/u013575812/article/details/50130307
时间复杂度O(n)。初始认为数组第一个数就是目标数(target)。之后遍历数组后面的元素,如果等于目标数,计数++; 否则计数--;如果发现目标数的计数<=0 说明当前目标数并不是最终的输出结果。更新目标数为当前遍历到的元素。遍历完数组所有元素之后 验证一下结果即可。
参考资料
https://blog.csdn.net/zjwreal/article/details/88607992
https://blog.csdn.net/u013575812/article/details/50130307
剑指offer28:找出数组中超过一半的数字。的更多相关文章
- 剑指offer.找出数组中重复的数字
题目: 给定一个长度为 n 的整数数组 nums,数组中所有的数字都在 0∼n−1 的范围内.数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次.请找出数组中任意一个重复的数 ...
- 【Java】 剑指offer(1) 找出数组中重复的数字
本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集 题目 在一个长度为n的数组里的所有数字都在0到n-1的范围内.数组中某些数字 ...
- 一起来刷《剑指Offer》-- 题目一:找出数组中重复的数字(Python多种方法实现)
数组中重复的数字 最近在复习算法和数据结构(基于Python实现),然后看了Python的各种"序列"--比如列表List.元组Tuple和字符串String,后期会写一篇博客介绍 ...
- 《剑指offer》第三_一题(找出数组中重复的数字,可改变数组)
// 面试题3(一):找出数组中重复的数字 // 题目:在一个长度为n的数组里的所有数字都在0到n-1的范围内.数组中某些数字是重复的,但不知道有几个数字重复了, // 也不知道每个数字重复了几次.请 ...
- 剑指offer35题:第一个只出现一次的字符+剑指offer55题:字符流中第一个不重复的字符+剑指offer51题:数组中重复的数字
在看剑指offer的时候,感觉这三个题目很像,都是用哈希表可以解决,所以把这三个题整理出来,以供复习. 剑指offer35题:第一个只出现一次的字符 题目描述:在字符串中找出第一个只出现一次的字符.如 ...
- 《剑指offer》旋转数组中的最小数字
本题来自<剑指offer> 旋转数组中的最小数字 题目: 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. 输入一个非减排序的数组的一个旋转,输出旋转数组的最小元素. 例 ...
- 【剑指Offer】旋转数组中的最小数字 解题报告(Python)
[剑指Offer]旋转数组中的最小数字 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://www.nowcoder.com/ta/coding-intervie ...
- 【剑指 Offer】03.数组中重复的数字
题目描述 找出数组中重复的数字. 在一个长度为 n 的数组 nums 里的所有数字都在 0-n-1 的范围内.数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次.请找出数组中 ...
- 【Offer】[3-1] 【找出数组中重复的数字】
题目描述 思路 Java代码 代码链接 题目描述 在一个长度为n的数组里的所有数字都在0~n-1的范围内.数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次. 请找出数组中任 ...
随机推荐
- Spring的xml中引入其他文件
引入db.properties <!--加载db.properties文件--> <context:property-placeholder location="class ...
- Bootstrap select 多选并获取选中的值
代码: <!DOCTYPE html><html> <head> <meta charset="UTF-8"> < ...
- Spring boot druid 的配置使用
依赖加入 <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artif ...
- 基于Redis的分布式锁到底安全吗(上)?
基于Redis的分布式锁到底安全吗(上)? 2017-02-11 网上有关Redis分布式锁的文章可谓多如牛毛了,不信的话你可以拿关键词“Redis 分布式锁”随便到哪个搜索引擎上去搜索一下就知道了 ...
- win10 eclipse连接虚拟机ubuntu中的hdfs
1.eclipse安装连接hadoop的插件hadoop-eclipse-plugin-2.6.0(注意自己hadoop的版本) 将该插件放在eclipse安装路径的plugins文件夹中. ps:我 ...
- Geometry and Appearances【转】
https://github.com/AnalyticalGraphicsInc/cesium/wiki/Geometry-and-Appearances Geometry and Appearanc ...
- 项目启动报错org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.wuhongyu.mapper.OrdersMapper.selectByExample
在用maven配置mybatis环境时出现此BindingExceptiony异常,发现在classes文件下没有mapper配置文件,应该是maven项目没有扫描到mapper包下的xml文件, 在 ...
- 模型稳定性指标—PSI
由于模型是以特定时期的样本所开发的,此模型是否适用于开发样本之外的族群,必须经过稳定性测试才能得知.稳定度指标(population stability index ,PSI)可衡量测试样本及模型开发 ...
- python : takes 0 positional arguments but 1 was given
def 的要加self, https://blog.csdn.net/u010269790/article/details/78834410
- Swagger 慢
Swagger 慢 - 国内版 Binghttps://cn.bing.com/search?FORM=U227DF&PC=U227&q=Swagger+%E6%85%A2 rest框 ...