原题链接在这里: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. erlang-jiffy 安装手记

    今天安装 erlang-jiffy 把握逼疯,不过最后还是成功了. 错误避免: rebar只能再英文目录下运行,如果编译jiffy的目录中有中文或其它unicode字符,将会出错 从git relea ...

  2. Java实现FTP文件上传与下载

    实现FTP文件上传与下载可以通过以下两种种方式实现(不知道还有没有其他方式),分别为:1.通过JDK自带的API实现:2.通过Apache提供的API是实现. 第一种方式 package com.cl ...

  3. WritePrivateProfileString()

    在我们写的程序当中,总有一些配置信息需要保存下来,以便完成程序的功能,最简单的办法就是将这些信息写入INI文件中,程序初始化时再读入.具体应用如下: 将信息写入.INI文件中 1.所用的WINAPI函 ...

  4. smarty模板中literal标签的使用

    在使用的时候把js等代码写在模板中就报错,加入literal标签后就正确了 <style> {literal} .tr_color{background-color: #9F88FF} { ...

  5. Webscan360的防御与绕过

    这两天给360做了一个webscan360的总结,结果补丁还没有出来,就被人公布到了91org上面,既然公开了,那我就做一个总结 首先我们贴上它最新的防御正则 \<.+javascript:wi ...

  6. WinEdt选项卡配置

    不小心把选项卡(标签页.多tab)整没了.搜了一下: 在工具栏点击右键可以发现配置.

  7. Solve one floodlight install problem

    参考: Floodlight安装 SDNLAB Floodlight官网 Installation Guide 问题: 在follow安装教程安装Floodlight的过程中,ant编译时出现了: [ ...

  8. 使用java代码,动态给TextView设置drawable

    Drawable country = context.getResources().getDrawable(drawableId); country.setBounds(0, 0, country.g ...

  9. 【IOS笔记】Resource Management in View Controllers

    Resource Management in View Controllers 视图控制器的资源管理 View controllers are an essential part of managin ...

  10. window.open()弹出窗口防止被禁

    window.open(),顾名思义,是指在当前浏览器窗口弹出另一个浏览器窗口. 因为多种原因,浏览对window.open弹出的窗口做了多方限制.限制不同,肯定会造成各浏览器弹出窗口的差异. 大部分 ...