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,很慢

  1. public class Solution {
  2. public int majorityElement(int[] nums) {
  3. Map map = new HashMap<Integer, Integer>();
  4. int len = nums.length;
  5. if (len == 1){
  6. return nums[0];
  7. }
  8. for (int num : nums){
  9. if (map.containsKey(num)){
  10. int count = (int) map.get(num);
  11. if ((count + 1) > len / 2){
  12. return num;
  13. } else {
  14. map.put(num, count + 1);
  15. }
  16. } else {
  17. map.put(num, 1);
  18. }
  19. }
  20. return -1;
  21. }
  22. }

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

1、排序sort

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

2、HashMap

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

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

  1. public int majorityElement3(int[] nums) {
  2. int count=0, ret = 0;
  3. for (int num: nums) {
  4. if (count==0)
  5. ret = num;
  6. if (num!=ret)
  7. count--;
  8. else
  9. count++;
  10. }
  11. return ret;
  12. }

4、位运算  Bit manipulation 

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

  1. public int majorityElement(int[] nums) {
  2. int[] bit = new int[32];
  3. for (int num: nums)
  4. for (int i=0; i<32; i++)
  5. if ((num>>(31-i) & 1) == 1)
  6. bit[i]++;
  7. int ret=0;
  8. for (int i=0; i<32; i++) {
  9. bit[i]=bit[i]>nums.length/2?1:0;
  10. ret += bit[i]*(1<<(31-i));
  11. }
  12. return ret;
  13. }

✡ 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. 清除Linux OS 缓存

    1.查看内存使用情况 [root@ip---- tpch_2_17_0]# free -m total used free shared buffers cached Mem: -/+ buffers ...

  2. rocksDB 安装问题简单介绍

    前一段时间准备测试rocksdb,按照帖子和官网的例子,在安装过程中遇到一些问题.这里给出的是在Ubuntu下安装python使用的版本. 首先,要感谢这些帖子对我的帮助: 1:http://tech ...

  3. Promise学习

    转自:http://www.cnblogs.com/lvdabao/p/es6-promise-1.html 去年6月份, ES2015正式发布(也就是ES6,ES6是它的乳名),其中Promise被 ...

  4. 当在浏览器输入一个url访问后发生了什么

    首先根据DNS获取该url的ip地址,ip地址的获取可能通过本地缓存,路由缓存等得到. 然后在网络层通过路由选择查找一条可达路径,最后利用tcp/ip协议来进行数据的传输. 其中在传输层将信息添加源端 ...

  5. TortoiseSVN文件夹及文件图标不显示解决方法

              由于自己的电脑是win7(64位)的,系统安装TortoiseSVN之后,其他的功能都能正常的使用,但是就是文件夹或文件夹的左下角就是不显示图标,这个问题前一段时间就遇到了(那个时 ...

  6. VMware下利用ubuntu13.04建立嵌入式开发环境之三

    系统环境建立完成后就要安装和配置嵌入式开始需要的工具和服务. 一般我们在交叉编译是需要的服务有:smb.tftp.telnet.nfs.ssh和x11等.下面一步步,介绍如何安装这些服务. 一.smb ...

  7. C++虚函数和虚函数表

    前导 在上面的博文中描述了基类中存在虚函数时,基类和派生类中虚函数表的结构. 在派生类也定义了虚函数时,函数表又是怎样的结构呢? 先看下面的示例代码: #include <iostream> ...

  8. what we do and how we behave

    It comes after a report last week revealed the "brutal" treatment of terror suspects by th ...

  9. 使用Excel 2007绘制甘特图

    本文将教大家如何使用Excel 2007制作甘特图.Excel并未提供甘特图类型,但还是可以绘制甘特图的,方法就是通过对堆积条形图类型进行自定义,使之显示任务.任务工期和层次结构. 下面的过程可帮助创 ...

  10. C#利用摄像头拍照功能实现

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...