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. 读入优化&输出优化

    读入优化 int read() { ; ') ; '; ') num=num*+c-'; return ff*num; } 输出优化 void write(int x) { ) { putchar(' ...

  2. 2015-2016-2《Java程序设计》团队博客3

    项目进展 这周就是对上周所列出的类进行具体实现.但是到目前为止还没有遇到一些实质性的问题.虽然感觉没有问题就是最大的问题,但是还是希望能够尽早发现bug并及时改掉. 目前已经完成前几个文件之间的架构, ...

  3. Git创建与合并分支,撤销修改

    git回滚到指定版本并推送到远程分支(撤销已提交的修改,并已push) git reset --hard <commit ID号> git push -f git回滚到上一个版本并推送到远 ...

  4. 性能测试指标:TPS,吞吐量,并发数,响应时间

    性能测试指标:TPS,吞吐量,并发数,响应时间 常用的网站性能测试指标有:TPS.吞吐量.并发数.响应时间.性能计数器等. 并发数并发数是指系统同时能处理的请求数量,这个也是反应了系统的负载能力. 响 ...

  5. pg_escape_string专用于转义数据库敏感字符

    (PHP 4 >= 4.2.0, PHP 5) pg_escape_string — 转义 text/char 类型的字符串 说明 string pg_escape_string ( strin ...

  6. [转]IntelliJ IDEA 2019 上手

    原文地址:https://www.jianshu.com/p/77f81d5fcf02 一.聊一聊Java IDE 作为程序员,经常会看到这么一类的话题:文本编辑器与IDE哪家强.常见的文本编辑器如E ...

  7. Java基础 throw 抛出异常后,用try...catch捕获

        JDK :OpenJDK-11      OS :CentOS 7.6.1810      IDE :Eclipse 2019‑03 typesetting :Markdown   code ...

  8. ISO/IEC 9899:2011 条款6——语言

    6 语言 6.1 标记法 1.在本条款中所使用的语法标记法.语义类别(非终结符)用斜体字指示,而字面量单词以及字符集成员(终结符)用粗体字指示.跟在一个非终结符后面的冒号(:)引出其定义.在单独的行中 ...

  9. 某表中字段值存在多个Gid逗号分开 取值拆分每个gid SQL多个逗号隔开的取值

    存在值信息 表值函数实现: --实现split功能 的函数 拆分 逗号分开的多个值 ),)) )) as begin declare @i int set @SourceSql=rtrim(ltrim ...

  10. cesharp 完美支持flash

    直接上代码: cefSettings.CefCommandLineArgs.Add("enable-npapi", "1"); //cefSettings.Ce ...