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 ...
随机推荐
- 高仿微信app (含有发红包,聊天,消息等)用到 Rxjava+Retrofit+MVP+Glide技术
https://github.com/GitLqr/LQRWeChat 技术很牛,可以看看
- 百度地图SnapshotReadyCallback截屏
今天碰到了地图截图的功能,不太会,查查资料知道怎么弄了,跟大家分享一下 直接上代码,弄了一个方法,将截取的图片上传至服务器,返回给我们图片路径 //获取地图截图 private void getscr ...
- js城市联动选择器
<html> <head> <META charset="utf8"> <script type="text/javascrip ...
- 【python】-- Socket粘包问题 ,解决粘包的几种方法、socket文件下载,md5值检验
上一篇随笔:“socket 接收大数据”,在win系统上能够运行,并且解决了大数据量的数据传输出现的问题,但是运行在linux系统上就会出现如下图所示的情况: 就是服务端两次发送给客户端的数据(第一次 ...
- HDU 4772 Zhuge Liang's Password (简单模拟题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4772 题面: Zhuge Liang's Password Time Limit: 2000/1000 ...
- IDEA 配置Tomcat 跑Jeecg项目
最近搞了个国人开发的开源项目,还不错,记录一下踩过得坑; 首先项目开源地址 下载就可以; 准备工作作者以介绍,不再详述; 1:我使用的IDEA作为开发工具- 首先导入pom.xml,下载依赖包(此过程 ...
- JETSON TK1 ~ 基于eclipse下开发ROS
此文档是在PC端开发后移植到TK1,并非在TK1上安装eclipse 官方使用IDE开发的文档: http://wiki.ros.org/IDEs 一:安装eclipse 1.下载eclipse安装包 ...
- NVM安装配置
http://www.kancloud.cn/summer/nodejs-install/71975 配置源 http://www.cnblogs.com/kaiye/p/4937191.html 安 ...
- Python运算和和表达式 学习笔记
光荣之路Python公开课第二讲 Python运算符和表达式. 一 Python运算符 Python运算符包括 算术运算符,赋值运算符,位运算符,逻辑运算符,身份运算符,成员运算符. 1. 算术运算符 ...
- UI控制滑杆插件
在线演示 本地下载