原题链接在这里:https://leetcode.com/problems/majority-element/

题目:

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.

题解:

Method 1:最容易想到的就是用HashMap 计数,数值大于n/2(注意不是大于等于而是大于),就是返回值。Time Complexity: O(n). Space: O(n).

Method 2: 用sort, 返回sort后array的中值即可. Time Complexity: O(n*logn). Space: O(1).

Method 3: 维护个最常出现值,遇到相同count++, 遇到不同count--, count为0时直接更改最常出现值为nums[i]. Time Complexity: O(n). Space: O(1).

AC Java:

 public class Solution {
public int majorityElement(int[] nums) {
/*
//Method 1, HashMap
HashMap<Integer, Integer> map = new HashMap<>();
for(int i = 0;i<nums.length; i++){
if(!map.containsKey(nums[i])){
map.put(nums[i],1);
}else{
map.put(nums[i],map.get(nums[i])+1);
}
} Iterator<Integer> it = map.keySet().iterator(); //Iterate HashMap
while(it.hasNext()){
int keyVal = it.next();
//There is an error here: Pay attentation, it is ">", but not ">="
//If we have three variables [3,2,3], ">=" will also return 2, 1>=3/2
if(map.get(keyVal) > nums.length/2){
return keyVal;
}
} return Integer.MIN_VALUE;
*/ /*Method 2, shortcut
Arrays.sort(nums);
return nums[nums.length/2];
*/ //Method 3
if(nums == null || nums.length == 0)
return Integer.MAX_VALUE;
int res = nums[0];
int count = 1;
for(int i = 1; i< nums.length; i++){
if(res == nums[i]){
count++;
}else if(count == 0){
res = nums[i];
count = 1;
}else{
count--;
}
}
return res; }
}

跟上Majority Element II.

类似Check If a Number Is Majority Element in a Sorted Array.

LeetCode Majority Element I && II的更多相关文章

  1. 2016.5.18——leetcode:Majority Element

    Majority Element 本题收获: 1.初步了解hash,nth_element的用法 2.题目的常规思路 题目: Given an array of size n, find the ma ...

  2. [LeetCode] Majority Element II 求众数之二

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...

  3. [LeetCode] Majority Element II 求大多数之二

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. Note: The a ...

  4. [LeetCode] Majority Element 求众数

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

  5. [LeetCode] Majority Element 求大多数

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

  6. LeetCode Majority Element I

    原题链接在这里:https://leetcode.com/problems/majority-element/ 题目: Given an array of size n, find the major ...

  7. 47.Majority Element I & II

    Majority Element I 描述 给定一个整型数组,找出主元素,它在数组中的出现次数严格大于数组元素个数的二分之一. You may assume that the array is non ...

  8. LeetCode——Majority Element

    在一个数组中找到主要的元素,也就是出现次数大于数组长度一半的元素.容易想到的方式就是计数,出现次数最多的就是majority element,其次就是排序,中间的就是majority element. ...

  9. LeetCode Majority Element Python

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

随机推荐

  1. C#SortedList排序列表怎么样逆序输出

    C#分别在集合库和泛型库中有共2套SortedList 以较新的泛型库为例,SortedList<int, string> l = new SortedList<int, strin ...

  2. CentoS 下安装gitlab

    curl https://raw.github.com/mattias-ohlsson/gitlab-installer/master/gitlab-install-el6.sh | bash 报错 ...

  3. [转载]CAsyncSocket及CSocket注解

    MFC疑难注解:CAsyncSocket及CSocket MFC对SOCKET编程的支持其实是很充分的,然而其文档是语焉不详的.以至于大多数用VC编写的功能稍复杂的网络程序,还是使用API的.故CAs ...

  4. java网络编程之UDP实例

    package Socket; import java.net.DatagramPacket; import java.net.InetAddress; public class Dgram { pu ...

  5. mysql 商品表的设计思路(面向对象建表:类与对象)

    学习地址 http://www.jtthink.com/course/play/352 商品通用信息主表:prop_main 不仔细多说,正常业务都会涉及到并且考虑的相对周全.常用字段[商品id][商 ...

  6. 学习之痛(数据库->存储过程和函数)

    存储过程和函数作为数据库的一部分,为什么是学习之痛. 项目实际开发,考虑性能和代码维护,绝对不用存储过程. 如果单纯自己写个小程序糊弄人玩,还可以写写. [学习] 在数据库中定义一些SQL语句集合,然 ...

  7. MongoDB命令模式下的条件过滤总结

    db.tb1.find() //查询全部,用it查看下一页.db.tb1.find({"age":1}) //查询年龄==1的记录db.tb1.find({},{"age ...

  8. Klayge 引擎的安装

    http://www.klayge.org/wiki/index.php?title=%E4%BE%8B%E5%AD%90%E7%A8%8B%E5%BA%8F&redirect=no& ...

  9. visual studio粘贴html代码.会自行添加一些未知代码(自动格式化)

    比如我们要粘贴这样一段代码到VS这里 <option {if empty($param.is_active)}selected=selected{/if}>请选择</option&g ...

  10. 使用photoshop,把图片背景变成透明

    鄙人使用的是photoshop CS6,win7系统,好了废话不多说,我们开始吧 1.打开photoshop,选择一个要编辑的图片 2.在右下角的图层面板上用鼠标左键快速双击背景图层为图片解锁 3.在 ...