1 题目描述

  数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}。由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2。如果不存在则输出0。

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;

  1. class Solution {
  2. public:
  3. int MoreThanHalfNum_Solution(vector<int> numbers) {
  4. // 哈希表存储某个数出现的次数 hash查询时间复杂度O(1);总时间复杂度O(n)
  5. map<int, int> m;
  6. for (int i = ; i < numbers.size(); ++i) {
  7. m[numbers[i]]+=;
  8. if(m[numbers[i]]>numbers.size()/)
  9. return numbers[i];
  10. }
  11. return ;
  12. }
  13. };

(2) 排序

  1. class Solution {
  2. public:
  3. int MoreThanHalfNum_Solution(vector<int> numbers) {
  4. sort(numbers.begin(),numbers.end());
  5. int key = numbers[numbers.size()/];
  6. int count = ;
  7. for (int i = ; i < numbers.size(); ++i) {
  8. if(key == numbers[i])
  9. ++ count;
  10. }
  11. if (count>numbers.size()/)
  12. return key;
  13. return ;
  14. }
  15. };

(3)  查找中位数 O(n)——完整代码

  1. #include <iostream>
  2. int Partition(int A[], int low, int high)
  3. {
  4. int pivot = A[low];
  5. while (low <high)
  6. {
  7. while (low<high && A[high] >= pivot) --high;
  8. A[low] = A[high];
  9. while (low<high && A[low] <= pivot) ++low;
  10. A[high] = A[low];
  11. }
  12. A[low] = pivot;
  13. return low;
  14. }
  15. int HalfData(int a[], int len)
  16. {
  17. int start = ;
  18. int end = len - ;
  19. int middle = len >> ;
  20. int index = Partition(a, start, end);
  21.  
  22. while (index != middle)
  23. {
  24. if (index > middle)
  25. {
  26. end = index - ;
  27. index = Partition(a, start, end);
  28. }
  29. else
  30. {
  31. start = index + ;
  32. index = Partition(a, start, end);
  33. }
  34. }
  35. return a[index];
  36. }
  37. int main()
  38. {
  39. int a[] = { , , , , , , , , };
  40. int len = ;
  41. int result = HalfData(a, );
  42. printf("result:%d\n", result);
  43.  
  44. system("pause");
  45. return ;
  46. }

4 C++完整代码  

  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. int MoreThanHalfNum(vector<int> numbers)
  7. {
  8. if (numbers.size() == )
  9. {
  10. return ;
  11. }
  12.  
  13. int target = numbers[];
  14. unsigned int cnt = ;
  15.  
  16. for (unsigned int i = ; i < numbers.size(); ++i)
  17. {
  18. if (target == numbers[i])
  19. {
  20. cnt++;
  21. }
  22. else
  23. {
  24. cnt--;
  25. }
  26.  
  27. if (cnt == )
  28. {
  29. cnt = ;
  30. target = numbers[i];
  31. }
  32. }
  33. cnt = ;
  34. for (unsigned int i = ; i < numbers.size(); ++i)
  35. {
  36. if (target == numbers[i])
  37. {
  38. cnt++;
  39. }
  40. }
  41.  
  42. if (cnt * > numbers.size())
  43. {
  44. return target;
  45. }
  46.  
  47. return ;
  48. }
  49.  
  50. int main(void)
  51. {
  52. int a[] = {,,,,,,,,};
  53. vector<int> v(a, a + );
  54.  
  55. cout<<MoreThanHalfNum(v)<<endl;
  56.  
  57. return ;
  58. }

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:找出数组中超过一半的数字。的更多相关文章

  1. 剑指offer.找出数组中重复的数字

    题目: 给定一个长度为 n 的整数数组 nums,数组中所有的数字都在 0∼n−1 的范围内.数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次.请找出数组中任意一个重复的数 ...

  2. 【Java】 剑指offer(1) 找出数组中重复的数字

    本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集 题目 在一个长度为n的数组里的所有数字都在0到n-1的范围内.数组中某些数字 ...

  3. 一起来刷《剑指Offer》-- 题目一:找出数组中重复的数字(Python多种方法实现)

    数组中重复的数字 最近在复习算法和数据结构(基于Python实现),然后看了Python的各种"序列"--比如列表List.元组Tuple和字符串String,后期会写一篇博客介绍 ...

  4. 《剑指offer》第三_一题(找出数组中重复的数字,可改变数组)

    // 面试题3(一):找出数组中重复的数字 // 题目:在一个长度为n的数组里的所有数字都在0到n-1的范围内.数组中某些数字是重复的,但不知道有几个数字重复了, // 也不知道每个数字重复了几次.请 ...

  5. 剑指offer35题:第一个只出现一次的字符+剑指offer55题:字符流中第一个不重复的字符+剑指offer51题:数组中重复的数字

    在看剑指offer的时候,感觉这三个题目很像,都是用哈希表可以解决,所以把这三个题整理出来,以供复习. 剑指offer35题:第一个只出现一次的字符 题目描述:在字符串中找出第一个只出现一次的字符.如 ...

  6. 《剑指offer》旋转数组中的最小数字

    本题来自<剑指offer> 旋转数组中的最小数字 题目: 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. 输入一个非减排序的数组的一个旋转,输出旋转数组的最小元素. 例 ...

  7. 【剑指Offer】旋转数组中的最小数字 解题报告(Python)

    [剑指Offer]旋转数组中的最小数字 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://www.nowcoder.com/ta/coding-intervie ...

  8. 【剑指 Offer】03.数组中重复的数字

    题目描述 找出数组中重复的数字. 在一个长度为 n 的数组 nums 里的所有数字都在 0-n-1 的范围内.数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次.请找出数组中 ...

  9. 【Offer】[3-1] 【找出数组中重复的数字】

    题目描述 思路 Java代码 代码链接 题目描述 在一个长度为n的数组里的所有数字都在0~n-1的范围内.数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次. 请找出数组中任 ...

随机推荐

  1. tiny web服务器源码分析

    tiny web服务器源码分析 正如csapp书中所记,在短短250行代码中,它结合了许多我们已经学习到的思想,如进程控制,unix I/O,套接字接口和HTTP.虽然它缺乏一个实际服务器所具备的功能 ...

  2. longitudinal models | 纵向研究 | mixed model

    A longitudinal study refers to an investigation where participant outcomes and possibly treatments o ...

  3. 在input内添加小图标或文字(元/月)等

    文字: <td class="formValue"> <div class="input-group"> <input id=&q ...

  4. for(auto i : v)遍历容器元素

    c++11的新特性,v是一个可遍历的容器或流,比如vector类型,i就用来在遍历过程中获得容器里的每一个元素. for(auto i:v) for(auto &i:v) 代码1:#inclu ...

  5. Java基础 for 单层循环示例

        JDK :OpenJDK-11      OS :CentOS 7.6.1810      IDE :Eclipse 2019‑03 typesetting :Markdown   code ...

  6. 真正的能理解CSS中的line-height,height与line-height

    https://blog.csdn.net/a2013126370/article/details/82786681 在最近的项目中,常常用到line-height,只是简单的理解为行高但并没有深层次 ...

  7. 使用 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 ...

  8. spring 配置事务管理器

    在Spring中数据库事务是通过PlatformTransactionManager进行管理的,jdbcTemplate是不能支持事务的,而能够支持事务的是org.springframework.tr ...

  9. array_map

    <?php //对数组中的每个元素做函数处理 $arr = array(,,,,,); function cheng($hah){ ; } var_dump(array_map('cheng', ...

  10. Swift细节记录<一>

    1.全局变量记录: import UIKit class HHTSwitchGlobalData: NSObject { var isWaiterAutoPop: Bool = true privat ...