剑指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的范围内.数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次. 请找出数组中任 ...
随机推荐
- tiny web服务器源码分析
tiny web服务器源码分析 正如csapp书中所记,在短短250行代码中,它结合了许多我们已经学习到的思想,如进程控制,unix I/O,套接字接口和HTTP.虽然它缺乏一个实际服务器所具备的功能 ...
- longitudinal models | 纵向研究 | mixed model
A longitudinal study refers to an investigation where participant outcomes and possibly treatments o ...
- 在input内添加小图标或文字(元/月)等
文字: <td class="formValue"> <div class="input-group"> <input id=&q ...
- for(auto i : v)遍历容器元素
c++11的新特性,v是一个可遍历的容器或流,比如vector类型,i就用来在遍历过程中获得容器里的每一个元素. for(auto i:v) for(auto &i:v) 代码1:#inclu ...
- Java基础 for 单层循环示例
JDK :OpenJDK-11 OS :CentOS 7.6.1810 IDE :Eclipse 2019‑03 typesetting :Markdown code ...
- 真正的能理解CSS中的line-height,height与line-height
https://blog.csdn.net/a2013126370/article/details/82786681 在最近的项目中,常常用到line-height,只是简单的理解为行高但并没有深层次 ...
- 使用 evo 工具评测 VI ORB SLAM2 在 EuRoC 上的结果
http://www.liuxiao.org/2017/11/%E4%BD%BF%E7%94%A8-evo-%E5%B7%A5%E5%85%B7%E8%AF%84%E6%B5%8B-vi-orb-sl ...
- spring 配置事务管理器
在Spring中数据库事务是通过PlatformTransactionManager进行管理的,jdbcTemplate是不能支持事务的,而能够支持事务的是org.springframework.tr ...
- array_map
<?php //对数组中的每个元素做函数处理 $arr = array(,,,,,); function cheng($hah){ ; } var_dump(array_map('cheng', ...
- Swift细节记录<一>
1.全局变量记录: import UIKit class HHTSwitchGlobalData: NSObject { var isWaiterAutoPop: Bool = true privat ...