【LEETCODE】35、169题, Majority Element
package y2019.Algorithm.array; import java.util.HashMap;
import java.util.Map; /**
* @ProjectName: cutter-point
* @Package: y2019.Algorithm.array
* @ClassName: MajorityElement
* @Author: xiaof
* @Description: 169. 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.
*
* Input: [3,2,3]
* Output: 3
*
* 获取重复数据达到n/2的数据
*
* @Date: 2019/7/2 10:50
* @Version: 1.0
*/
public class MajorityElement { //自己的解法,效率极底。。。
public int solution(int[] nums) {
//我们考虑用hash的原理做
Map<Integer, Integer> map = new HashMap();
for(int i = 0; i < nums.length; ++i) {
if(map.containsKey(nums[i])) {
map.put(nums[i], map.get(nums[i]) + 1);
} else {
map.put(nums[i], 1);
}
}
//取出出现次数最大的
Integer result = null;
int maxTimes = 0;
for(Map.Entry<Integer, Integer> entry : map.entrySet()) {
if(entry.getValue() > maxTimes) {
maxTimes = entry.getValue();
result = entry.getKey();
}
} return result;
} //我们换个思路,这题majority element always exist in the array. 必定存在这个元素
//那么我们只要求出最大出现次数的元素,那么就一定满足要求
public int solution2(int[] nums) { int count = 1;
int result = nums[0]; for(int i = 1; i < nums.length; ++i) {
if(count == 0) {
//当前元素出现次数以及衰减完毕
result = nums[i]; //换新元素
count++;
} else if (nums[i] == result) {
//如果重复出现
count++;
} else {
count--;
}
} return result;
} public static void main(String args[]) { int pres[] = {2,2,1,1,1,2,2,1,1};
System.out.println(new MajorityElement().solution2(pres)); } }
【LEETCODE】35、169题, Majority Element的更多相关文章
- LeetCode(169)Majority Element
题目 Given an array of size n, find the majority element. The majority element is the element that app ...
- Leetcode # 169, 229 Majority Element I and II
Given an array of size n, find the majority element. The majority element is the element that appear ...
- 【LeetCode 169】Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
- [UCSD白板题] Majority Element
Problem Introduction An element of a sequence of length \(n\) is called a majority element if it app ...
- LeetCode算法题-Majority Element(Java实现)
这是悦乐书的第181次更新,第183篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第40题(顺位题号是169).给定大小为n的数组,找到数组中出现次数超过n/2的元素.假 ...
- leetcode第25题--Remove Element
problem: Given an array and a value, remove all instances of that value in place and return the new ...
- 【LeetCode 229】Majority Element II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...
- Leetcode之分治法专题-169. 求众数(Majority Element)
Leetcode之分治法专题-169. 求众数(Majority Element) 给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是 ...
- LeetCode Javascript实现 169. Majority Element 217. Contains Duplicate(两个对象比较是否相等时,如果都指向同一个对象,a==b才是true)350. Intersection of Two Arrays II
169. Majority Element /** * @param {number[]} nums * @return {number} */ var majorityElement = funct ...
随机推荐
- pgloader 学习(六) 加载csv 数据
关于加载的配置参数都是使用comand file command file 参考格式 LOAD CSV FROM 'GeoLiteCity-Blocks.csv' WITH ENCODING iso- ...
- 3-开发共享版APP(接入指南)-设备接入说明:使用隐藏配置
https://www.cnblogs.com/yangfengwu/p/11273226.html 该APP安装包下载链接: http://www.mnif.cn/appapk/IotDevelop ...
- javascript 百度地图无秘钥(appkey)创建marker标记地图
创建简单的marker地图不一定需要去百度地图申请key,简单代码实现marker地图,效果如图: html代码如下,代码中的baidu.api.js参考后面的隐藏代码: <!DOCTYPE h ...
- delphi调用https接口
delphi调用http接口直接使用idhttp就可以了,但是调用https接口的时候就需要和IdSSLIOHandlerSocket1控件一起使用. 截图中是两个控件的具体配置,需要注意的是IdSS ...
- [golang]Golang实现高并发的调度模型---MPG模式
Golang实现高并发的调度模型---MPG模式 传统的并发形式:多线程共享内存,这也是Java.C#或者C++等语言中的多线程开发的常规方法,其实golang语言也支持这种传统模式,另外一种是Go语 ...
- 微信小程序 Flex局部元素被挤压问题
关于Flex布局不在此处赘述,需要了解的可以查阅官方文档:基本的布局方法——Flex布局 当使用Flex布局,想实现如下图1的效果时,代码编写如下: 图1: <!-- wxml文件 --> ...
- 牛顿插值法(c++)
X Y 0.40 0.41075 0.55 0.57815 0.65 0.69675 0.80 0.88811 0.90 1.02652 1.05 1.25382 #include using nam ...
- golang调用 exec命令 出现too many open files
systemd 启动的服务, 跟系统的ulimit 没有关系. 大概的意思就是通过systemd启动的服务,不会使用ulimit中的配置,需要在systemd中或者service配置文件中定义,可以通 ...
- mysql-connector-java(6.0以上)的时差问题
一.背景 通过mybatis日志观察插入数据库的时间为当前时间,但是打开数据库表发现时间滞后了8个小时. 二.推论及解决 很容易猜到这是时区的问题. 三.最后找到的问题点如下: jdbc:mysql: ...
- 《你必须知道的javascript(上)》- 2.this与对象原型
1 关于this 1.1 为什么使用this 随着你的使用模式越来越复杂,显式传递上下文对象会让代码变得越来越混乱,使用this则不会这样.当我们介绍对象和原型时,你就会明白函数可以自动引用合适的上下 ...