给定一个大小为 n 的数组,找到其中的众数。众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素。
你可以假设数组是非空的,并且数组中的众数永远存在。

详见:https://leetcode.com/problems/majority-element/description/

Java实现:

方法一:

  1. class Solution {
  2. public int majorityElement(int[] nums) {
  3. int n=nums.length;
  4. if(n==0||nums==null){
  5. return -1;
  6. }
  7. int num=nums[0];
  8. int cnt=1;
  9. for(int val:nums){
  10. if(val==num){
  11. ++cnt;
  12. }else{
  13. --cnt;
  14. if(cnt==0){
  15. num=val;
  16. cnt=1;
  17. }
  18. }
  19. }
  20. cnt=0;
  21. for(int val:nums){
  22. if(val==num){
  23. ++cnt;
  24. }
  25. }
  26. if(2*cnt>n){
  27. return num;
  28. }
  29. return -1;
  30. }
  31. }

方法二:

  1. class Solution {
  2. public int majorityElement(int[] nums) {
  3. int n=nums.length;
  4. if(n==0||nums==null){
  5. return -1;
  6. }
  7. int low=0;
  8. int high=n-1;
  9. int mid=n>>1;
  10. int index=partition(nums,low,high);
  11. while(index!=mid){
  12. if(index>mid){
  13. high=index-1;
  14. index=partition(nums,low,high);
  15. }else if(index<mid){
  16. low=index+1;
  17. index=partition(nums,low,high);
  18. }
  19. }
  20. int num=nums[mid];
  21. int cnt=0;
  22. for(int val:nums){
  23. if(val==num){
  24. ++cnt;
  25. }
  26. }
  27. if(2*cnt>n){
  28. return num;
  29. }
  30. return -1;
  31. }
  32. private int partition(int[] nums,int low,int high){
  33. int pivot=nums[low];
  34. while(low<high){
  35. while(low<high&&nums[high]>=pivot){
  36. --high;
  37. }
  38. nums[low]=nums[high];
  39. while(low<high&&nums[low]<=pivot){
  40. ++low;
  41. }
  42. nums[high]=nums[low];
  43. }
  44. nums[low]=pivot;
  45. return low;
  46. }
  47. }

python实现:

参考:http://www.cnblogs.com/grandyang/p/4233501.html

169 Majority Element 求众数 数组中出现次数超过一半的数字的更多相关文章

  1. [LeetCode169]Majority Element求一个数组中出现次数大于n/2的数

    题目: Given an array of size n, find the majority element. The majority element is the element that ap ...

  2. php实现求数组中出现次数超过一半的数字(isset($arr[$val]))(取不同数看剩)(排序取中)

    php实现求数组中出现次数超过一半的数字(isset($arr[$val]))(取不同数看剩)(排序取中) 一.总结 1.if(isset($arr[$val])) $arr[$val]++; //1 ...

  3. Leetcode - 剑指offer 面试题29:数组中出现次数超过一半的数字及其变形(腾讯2015秋招 编程题4)

    剑指offer 面试题29:数组中出现次数超过一半的数字 提交网址: http://www.nowcoder.com/practice/e8a1b01a2df14cb2b228b30ee6a92163 ...

  4. 九度OJ 1370 数组中出现次数超过一半的数字

    题目地址:http://ac.jobdu.com/problem.php?pid=1370 题目描述: 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组{1,2 ...

  5. 数组中出现次数超过一半的数字 -java

    数组中出现次数超过一半的数字 -java 方法一: 数组排序,然后中间值肯定是要查找的值. 排序最小的时间复杂度(快速排序)O(NlogN),加上遍历. 方法二: 使用散列表的方式,也就是统计每个数组 ...

  6. 《剑指offer》— JavaScript(28)数组中出现次数超过一半的数字

    数组中出现次数超过一半的数字 题目描述 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于数字2在数组中出现了5次,超 ...

  7. 剑指Offer - 九度1370 - 数组中出现次数超过一半的数字

    剑指Offer - 九度1370 - 数组中出现次数超过一半的数字2013-11-23 03:55 题目描述: 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组 ...

  8. 剑指Offer(二十八):数组中出现次数超过一半的数字

    剑指Offer(二十八):数组中出现次数超过一半的数字 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn. ...

  9. 剑指 Offer 39. 数组中出现次数超过一半的数字

    剑指 Offer 39. 数组中出现次数超过一半的数字 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字. 你可以假设数组是非空的,并且给定的数组总是存在多数元素. 示例 1: 输入: [ ...

随机推荐

  1. 【机器学习具体解释】SVM解二分类,多分类,及后验概率输出

    转载请注明出处:http://blog.csdn.net/luoshixian099/article/details/51073885 CSDN−勿在浮沙筑高台 支持向量机(Support Vecto ...

  2. Codeforces 344B Simple Molecules

    #include<bits/stdc++.h> using namespace std; int main() { int a,b,c; scanf("%d%d%d", ...

  3. 解决pycharm下安装reportLab报错的问题

    在利用pycharm中自带的第三方安装工具安装reportLab时提示安装失败.失败的原因是缺失第三方扩展包.经过查阅查阅资料了解到一些python的第三方扩展包是需要python-dev支持的.我装 ...

  4. 原生js 平滑滚动到页面的某个位置

    window.scrollTo() 语法1:  window.scrollTo(x-coord,y-coord) x-coord 是文档中的横轴坐标. y-coord 是文档中的纵轴坐标. 例子: w ...

  5. vs2013发布网站提示 “未能将文件**复制到**”

    原因:年久失修,原来在项目中的一些文件给删掉或移除了 解决方法:打开.csproj文件(记事本打开),把提示的文件给删除掉.

  6. Hackrank Kingdom Division 树形DP

    题目链接:传送门 题意: 给你一棵树,n个点 每个点可以染成红色和蓝色 但是红色的点与其相邻的点中必须有红色节点,蓝色也是 问你有多少种染色的方案 题解: 树形dp 先转化为有根树,取1为根 设定dp ...

  7. Codeforces Round #419 (Div. 1) C. Karen and Supermarket 树形DP

    C. Karen and Supermarket     On the way home, Karen decided to stop by the supermarket to buy some g ...

  8. HashMap的数据机构是什么样的?

    参考:http://www.cnblogs.com/ITtangtang/p/3948406.html

  9. YTU 2918: Shape系列-4

    2918: Shape系列-4 时间限制: 1 Sec  内存限制: 128 MB 提交: 276  解决: 232 题目描述 小聪送给小亮和小华的形状他们都很喜欢,小亮和小华非要比一下他们两个的形状 ...

  10. 几个最短路径算法Floyd、Dijkstra、Bellman-Ford、SPFA的比较(转)

    几大最短路径算法比较 几个最短路径算法的比较:Floyd        求多源.无负权边(此处错误?应该可以有负权边)的最短路.用矩阵记录图.时效性较差,时间复杂度O(V^3).       Floy ...