LeetCode Majority Element I
原题链接在这里: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;
}
}
类似Check If a Number Is Majority Element in a Sorted Array.
LeetCode Majority Element I的更多相关文章
- 2016.5.18——leetcode:Majority Element
Majority Element 本题收获: 1.初步了解hash,nth_element的用法 2.题目的常规思路 题目: Given an array of size n, find the ma ...
- [LeetCode] Majority Element II 求众数之二
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...
- [LeetCode] Majority Element 求众数
Given an array of size n, find the majority element. The majority element is the element that appear ...
- LeetCode Majority Element I && II
原题链接在这里:Majority Element I,Majority Element II 对于Majority Element I 来说,有多重解法. Method 1:最容易想到的就是用Hash ...
- [LeetCode] Majority Element II 求大多数之二
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. Note: The a ...
- [LeetCode] Majority Element 求大多数
Given an array of size n, find the majority element. The majority element is the element that appear ...
- LeetCode——Majority Element
在一个数组中找到主要的元素,也就是出现次数大于数组长度一半的元素.容易想到的方式就是计数,出现次数最多的就是majority element,其次就是排序,中间的就是majority element. ...
- LeetCode Majority Element Python
Given an array of size n, find the majority element. The majority element is the element that appear ...
- Leetcode: Majority Element II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...
随机推荐
- local variable 'xxx' referenced before assignment(犯过同样的错)
这个问题很囧,在外面定义了一个变量 xxx ,然后在Python的一个函数里面引用这个变量,并改变它的值,结果报错local variable 'xxx' referenced before assi ...
- vptr
#include <stdio.h> class Point3d { public: virtual ~Point3d(){} public: static Point3d origin; ...
- Office 365系列(-)
昨天参加上海微软TechED技术大会,看见很多传说中的大牛,听了涂曙光老师等人的讲座,激情澎湃啊,看见他们对技术以及程序员社区的投入及激情,十分敬佩.自己搞IT行业也已经10多年了,平常都很少写博客和 ...
- 关于KMP算法的感想
今天,看了KMP,首先是在网上看的,看了很久没看懂,有很多思想,很多next的推导,就相当于很多的版本,后来,去看了<<大话数据结构>>这本书,才看懂,这KMP的神奇之处,这本 ...
- 九度OJ 1250:矩阵变换 (矩阵运算)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:95 解决:47 题目描述: 对于一个整数矩阵,存在一种运算,对矩阵中任意元素加一时,需要其相邻(上下左右)某一个元素也加一, 现给出一正数矩 ...
- mysql语句, 空的 order by , 空的 where
SQL语句里, 空的 where, where 1 AND status=1 空的 order by, order by null, updatetime desc 这种空值的情况, 是很有用处的: ...
- go语言之并发编程 channel(1)
单向channel: 单向通道可分为发送通道和接收通道.但是无论哪一种单向通道,都不应该出现在变量的声明中,假如初始化了这样一个变量 var uselessChan chan <- int =m ...
- python基础4 ----字符编码
python基础---字符编码 一.了解字符编码 1. 文本编辑器存取文件的原理(nodepad++,pycharm,word) 打开编辑器就打开了启动了一个进程,是在内存中的,所以在编辑器编写的内容 ...
- value too great for base (error token is "08")
shell 中,经常有定时任务, 这时候shell脚本中一般会对时间进行一些判断,或者相关逻辑的操作 这时候,如果你获取的小时或者分钟是08,09,如果要再对其进行运算符或者比较的话,就会报标题的错误 ...
- 第14条:尽量用异常来表示特殊情况,而不要返回Nono
核心知识点: 1.用None这个返回值来表示特殊意义的函数,很容易使调用者犯错,因为None和0以及空字符串之类的值,在条件表达式里都会评估为False. 2.两种方法:二元法:将异常抛给上一级直接报 ...