Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.

You may assume that the array is non-empty and the majority element always exist in the array.

给定一个数组,求其中权制最大的元素,(该元素出现超过了一半次数)。

直观想法,HashMap,很慢

public class Solution {
public int majorityElement(int[] nums) {
Map map = new HashMap<Integer, Integer>();
int len = nums.length;
if (len == 1){
return nums[0];
}
for (int num : nums){
if (map.containsKey(num)){
int count = (int) map.get(num);
if ((count + 1) > len / 2){
return num;
} else {
map.put(num, count + 1);
}
} else {
map.put(num, 1);
}
}
return -1;
}
}

discuss上面看到了这道题非常完善的总结:

1、排序sort

public int majorityElement1(int[] nums) {
Arrays.sort(nums);
return nums[nums.length/2];
}

2、HashMap

3、Moore’s voting algorithm(最佳算法)

很巧妙,时间O(n)  ,空间O(1),就是记录当前元素以及当前元素的“胜出”数量(比其他元素多几个)。

public int majorityElement3(int[] nums) {
int count=0, ret = 0;
for (int num: nums) {
if (count==0)
ret = num;
if (num!=ret)
count--;
else
count++;
}
return ret;
}

4、位运算  Bit manipulation 

通过记录每一位上的数字来得出的结果,相比之下,还是第三种算法更好。

public int majorityElement(int[] nums) {
int[] bit = new int[32];
for (int num: nums)
for (int i=0; i<32; i++)
if ((num>>(31-i) & 1) == 1)
bit[i]++;
int ret=0;
for (int i=0; i<32; i++) {
bit[i]=bit[i]>nums.length/2?1:0;
ret += bit[i]*(1<<(31-i));
}
return ret;
}

✡ leetcode 169. Majority Element 求出现次数最多的数 --------- java的更多相关文章

  1. leetcode 169. Majority Element 、229. Majority Element II

    169. Majority Element 求超过数组个数一半的数 可以使用hash解决,时间复杂度为O(n),但空间复杂度也为O(n) class Solution { public: int ma ...

  2. Leetcode#169. Majority Element(求众数)

    题目描述 给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. 示例 1: 输入: [3,2,3] ...

  3. 23. leetcode 169. Majority Element

    169. Majority Element Given an array of size n, find the majority element. The majority element is t ...

  4. [LeetCode] 169. Majority Element 多数元素

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  5. leetcode——169 Majority Element(数组中出现次数过半的元素)

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  6. leetcode 169 Majority Element 冰山查询

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  7. Java for LeetCode 169 Majority Element

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  8. Java [Leetcode 169]Majority Element

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

  9. LeetCode 169. Majority Element (众数)

    Given an array of size n, find the majority element. The majority element is the element that appear ...

随机推荐

  1. AliOS编译安装MyRocks

    MyRocks是facabook版将自主研发的MySQL分支,其源码位于为:https://github.com/facebook/mysql-5.6/ 首先需要安装以下: sudo yum inst ...

  2. 基本矩阵运算的Java实现

      一: 矩阵的加法与减法 规则:矩阵的加法与减法要求两个矩阵的行列完全相等,方可以完成两个矩阵的之间的运算. 举例说明如下 二:矩阵的乘法 规则:矩阵的乘法要求两个矩阵符合A(mx k),  B( ...

  3. JavaScript之全局变量和隐式全局变量

    通过var创建的全局变量(任何函数之外的程序中创建)是不能被删除的. 无var创建的隐式全局变量(无视是否在函数中创建)是能被删除的.

  4. 我收录整理的优秀OC技术类文章

        自定义导航按钮UIBarButtonItem   关于导航栏的六个小技巧   ios开发的一些小技巧篇一 制作一个可以滑动操作的 Table View Cell - IOS - 伯乐在线 一个 ...

  5. kendo-ui学习笔记——题记

    1.Kendo UI基于最新技术HTML5.CSS3和JavaScript标准设计开发.2.官方网址:http://www.kendoui.com/3.API网上查阅英文版网址:http://docs ...

  6. win7配上的网关会自动消失?解决

    前几天遇见一台计算机,发现手动设置的ip和网关等...在使用了一会就变成,网关丢失,其他不变...奇怪啊...第一次遇见.后来找了一下.有答案了. 先将客户端卸载掉,再打开网络和共享中心-->本 ...

  7. url中

    url中汉字被转换为UTF-8,通过此网址可以解析UTF-8编码.在地址栏中输入文字或者其它的,则可能输入法或者是相关的程序进行解析. http://www.mytju.com/classcode/t ...

  8. Android开源框架:Universal-Image-Loader解析(四)TaskProcess

    Universal-Image-Loader中,对Task的处理有两种方法:FIFO,LIFO 在core/assist下的deque包中,其主要是定义了LIFOLinkedBlockingDeque ...

  9. K均值聚类算法的MATLAB实现

    1.K-均值聚类法的概述    之前在参加数学建模的过程中用到过这种聚类方法,但是当时只是简单知道了在matlab中如何调用工具箱进行聚类,并不是特别清楚它的原理.最近因为在学模式识别,又重新接触了这 ...

  10. Django和Flask对于URL尾斜杠(back slash)的处理

    最近在看Flask,其中提到了对于URL尾斜杠的处理.感觉算是一个需要注意的地方吧,就和Django的处理方式来进行一个简单的对比. 首先说下什么是尾斜杠. http://www.baidu.com/ ...