LeetCode Majority Element I && II
原题链接在这里: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 && II的更多相关文章
- 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 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 求大多数
Given an array of size n, find the majority element. The majority element is the element that appear ...
- LeetCode Majority Element I
原题链接在这里:https://leetcode.com/problems/majority-element/ 题目: Given an array of size n, find the major ...
- 47.Majority Element I & II
Majority Element I 描述 给定一个整型数组,找出主元素,它在数组中的出现次数严格大于数组元素个数的二分之一. You may assume that the array is non ...
- 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 ...
随机推荐
- iOS移动开发周报-第24期
iOS移动开发周报-第24期 [摘要]:本期iOS移动开发周报带来如下内容:苹果更新了iTunes Connect的设计.UIKit Dynamics 教程:抛掷 Views.iOS APP 架构漫谈 ...
- 8.0 Qweb 报表编写步骤
8.0 采用的是Qweb报表,摒弃了7.0中的RML报表. 1.首先在xml文件中注册一个报表: <report id="qweb_test_report" model=&q ...
- 几种进入mysql的方法
1.首先mysql服务得打开(运行cmd命令也可以net start mysql) 2.运行cmd,打开mysq 3.mysql命令行打开mysql 4.图形管理工具,如phpMyadmin
- filesize返回的是以字节为单位的数据也可以使之处理数字
1.文件的属性信息获取 首先文件具有类型,在linux下边,有block(块设备,如磁盘分区.CD-ROM).char(以字符为输入的设备,如键盘.打印机).dir(目录类型,目录也是文件的一种).f ...
- 【翻译】CEDEC2014跨世代多平台并行开发PS4版如龙维新开发的一年
本篇PPT讲述的是如龙4的开发过程中,集中在PS3和PS4并行开发中所遇到和解决的一些问题.如64位指针,DX9向DX11移植API的问题,以及在PS4上使用并行渲染在1080P下让FPS达到60等. ...
- [收藏] 关于解决“进程com.android.phone意外停止”的方法 (未尝试)
很多机油反应有这个情况,本人费劲九牛20虎之力终于克服之,这个现象一般出现在刚刷完系统会出现,甚至你怎么刷ROM这个现象依旧存在(崩溃不?)~~~有位机油刷了这个系统也出现了http://samsun ...
- Bootstrap页面布局10 - BS代码
网页中标记代码内容使用code和pre标签 要在HTML中显示标签本来的样子需要转化为实体 在此附上百度的实体字符:http://baike.baidu.com/view/4757776.htm#3 ...
- RedirectResult,RedirectToRoute
RedirectResult:运行重新导向到其他网址,在RedirectResult的内部,基本上还是以Response.Redirect方法响应HTTP 302暂时导向. eg: public Ac ...
- LoadRunner系统架构简介
1.LoadRunner系统架构简介 LoadRunner是通过创建虚拟用户来代替真实实际用户来操作客户端软件比如Internet Explorer,来向IIS.Apache等Web服务器发送HTTP ...
- the core or essence of a computer
COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION The ALU is that part ...