原题链接在这里: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. PHP为什么会被认为是草根语言?

    PHPer是草根吗? 从PHP诞生之日起,PHP就开始在Web应用方面为广大的程序员服务.同时,作为针对Web开发量身定制的脚本语言,PHP一直秉承简单.开源的 思想,这也使得PHP得以快速的发展,并 ...

  2. SQL Server 中的游标(cursor)

    http://www.cnblogs.com/Dlonghow/archive/2009/05/14/1456910.html 在数据库中,游标是一个十分重要的概念.游标提供了一种对从表中检索出的数据 ...

  3. 我装GitHub的过程

    GitHub是老师推荐的没正真的使用过,这次安装也是按提示的,不知对否,且还没使用,只是记录一下自己的过程.我是在线安装的. 1.下载GitHub安装问价,双击开始安装 2.出现的可能是系统相关配置吧 ...

  4. 如何安装ESXi的补丁

    1.进入维护模式 2.通过vSphere Client上传补丁 3.开SSH后使用Putty连接 4.esxcli software vib install -d="/vmfs/volume ...

  5. 项目部署到Tomat报错:jar not loaded.See Servlet Spec 2.3, section 9.7.2. Offending

    项目部署到Tomcat报这样的异常: Java代码   jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: ja ...

  6. Apache Storm 衍生项目之2 -- Trident-ML

    欢迎转载,转载请注明出处,徽沪一郎,谢谢. 楔子 或许谈起storm是大数据实时计算框架已经让你不明觉厉,如果说storm还可以跟机器学习算法(ml)有机的结合在一起,是不是更加觉着高大尚呢.trid ...

  7. PHP 命名空间总结

    PHP 5.3 及以上版本中引入了命名空间 的概念. notes: 1. 在 PHP 中,命名空间用来解决在编写 类库 或 应用程序 时创建 可重用 的 代码如 类 或 函数 时碰到的两类问题: ① ...

  8. cURL 学习笔记与总结(1)概念

    概念: cURL(Client URL Library Functions)is a command line tool for transfering data with URL syntax(使用 ...

  9. PHP 支持中文目录和文件的的遍历:文件编码转换

    在使用 readdir() 遍历指定目录时,使中文目录和文件名都正常显示需要使用 iconv() 进行文件编码转换: <?php header("Content-type:text/h ...

  10. PHP+jQuery 注册模块的改进之二:激活链接的URL设置与有效期

    接<PHP+jQuery 注册模块的改进之一>继续修改: ①在注册成功后返回登录邮件页面( maillogin.php ),在页面中用户可以点击链接跳转到自己注册邮箱的登录页面,可以再次发 ...