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的更多相关文章

  1. LeetCode(169)Majority Element

    题目 Given an array of size n, find the majority element. The majority element is the element that app ...

  2. 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 ...

  3. 【LeetCode 169】Majority Element

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  4. [UCSD白板题] Majority Element

    Problem Introduction An element of a sequence of length \(n\) is called a majority element if it app ...

  5. LeetCode算法题-Majority Element(Java实现)

    这是悦乐书的第181次更新,第183篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第40题(顺位题号是169).给定大小为n的数组,找到数组中出现次数超过n/2的元素.假 ...

  6. leetcode第25题--Remove Element

    problem: Given an array and a value, remove all instances of that value in place and return the new ...

  7. 【LeetCode 229】Majority Element II

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...

  8. Leetcode之分治法专题-169. 求众数(Majority Element)

    Leetcode之分治法专题-169. 求众数(Majority Element) 给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是 ...

  9. 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 ...

随机推荐

  1. [RN] React Native 关闭所有黄色警告

    [RN] React Native 关闭所有黄色警告 console.ignoredYellowBox = ['Warning: BackAndroid is deprecated. Please u ...

  2. MATLAB 可以画的各种类型的图总结

    MATLAB® 提供了各种可用来绘制数据图的函数.下表对常见的图形函数进行了分类和说明. 线图 数据分布图 离散数据图 极坐标图 等高线图 向量场 plot area stairs polarplot ...

  3. 谈下sublime和vscode

    sublime用了好多年一直没有换掉 它的体积小运行启动快用着一直很顺手,而且也有不少的插件可以去安装, 而相对于vscode 开始觉得他启动慢有点笨重的感觉,可能是我电脑原因用着卡卡的所以一直没换成 ...

  4. 为什么集合类没有实现Cloneable和Serializable接口

    为什么集合类没有实现Cloneable和Serializable接口? 答:克隆(cloning)或者序列化(serialization)的语义和含义是跟具体的实现相关的.因此应该由集合类的具体实现类 ...

  5. PHP cURL 超时设置 CURLOPT_CONNECTTIMEOUT 和 CURLOPT_TIMEOUT 的区别

    PHP cURL 的超时设置有两个 CURLOPT_CONNECTTIMEOUT 和 CURLOPT_TIMEOUT,他们的区别是: CURLOPT_CONNECTTIMEOUT 用来告诉 PHP 在 ...

  6. HearthBuddy投降插件2019-11-01的使用

    在AutoConcede.cs文件中找到如下代码 private List<int> _winList = new List<int> {0, 2, 4, 6, 8}; 现在的 ...

  7. mysql查询、子查询、连接查询

    mysql查询.子查询.连接查询 一.mysql查询的五种子句 where子句(条件查询):按照“条件表达式”指定的条件进行查询. group by子句(分组):按照“属性名”指定的字段进行分组.gr ...

  8. 【mybatis源码学习】ResultMap查询结果映射

    一.ResultMap包含的元素 constructor - 用于在实例化类时,注入结果到构造方法中 idArg - ID 参数:标记出作为 ID 的结果可以帮助提高整体性能 arg - 将被注入到构 ...

  9. 企业架构 Red Hat Drools KIE Project 三大核心产品

    美团放弃Drools自研规则引擎: https://blog.csdn.net/qq_18603599/article/details/80767912 Drools rule engine虽然好,但 ...

  10. c#修改webservice 的地址和端口(修改配置文件)

    修改服务引用的地址和端口有两种方法: 1.直接修改配置文件web.config 2.动态修改: 右击转到实现 发现五个构造方法,通过第三个构造方法即可进行URL的初始化 IIS修改IP地址后 直接在初 ...