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. Permutations II 再分析

    记得第一遍做这题的时候其实是没什么思路的,但是第二次的时候,我已经有"结果空间树"的概念了.这时候再看https://oj.leetcode.com/problems/permut ...

  2. USB协议规范学习(一)

    什么是USB OHCI规范? OHCI(Open HCI)是目前使用比较广泛的三种USB主机控制器规范之一.USB体系结构是由四个主要部分组成:客户软件/USB驱动,主机控制器驱动(HCD),主机控制 ...

  3. asp.net中关于Microsoft 信息完整性、隐私性等集成信息安全服务服务 integrated security=SSPI

    string strConn=@"server=(local)\SQLExpress;database=AdventureWorks;integrated security=SSPI&quo ...

  4. CSS之transition过渡练习

    代码: <!DOCTYPE html><html><head> <title>transition</title> <meta cha ...

  5. JAVA第二次作业展示与学习心得

    JAVA第二次作业展示与学习心得 在这一次作业中,我学习了复选框,密码框两种新的组件,并通过一个邮箱登录界面将两种组件运用了起来.具体的使用方法和其他得组件并没有什么大的不同. 另外我通过查阅资料使用 ...

  6. 如何通过JDBC访问数据库

    Java数据库连接(JDBC)用与在Java程序中实现数据库操作功能,它提供了执行SQL语句.访问各种数据库的方法,并为各种不同的数据库提供统一的操作接口,java.sql包中包含了JDBC操作数据库 ...

  7. 如何布局包含Image和Title的UIButton

    UIButton中的titleEdgeInsets和imageEdgeInsets可以管理button中image和title的布局.如果对其理解不够深入,用纯数字进行布局管理,经过不断的调试,还是能 ...

  8. AJAX请求WebService

    1.WebService代码 [WebMethod] [ScriptMethod(UseHttpGet = false)] public string GetObject() { User user ...

  9. theano 实现图像局部对比度归一化

    很多时候我们需要对图像进行局部对比度归一化,比如分块CNN的预处理阶段.theano对此提供了一些比较方便的操作. 局部归一化的一种简单形式为: 其中μ和σ分别为局部(例如3x3的小块)的均值和标准差 ...

  10. ansible安装httpd

    --- - hosts: web   tasks:       - name: "INSTALL"         yum: name={{ item  }} state=pres ...