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. edgedb 开发环境运行

    以下是一篇来自官方的edgedb 开发环境搭建说明,实际上我以前自己也摸索过一个,基本方法一样,一些是官方的做一个 简单的记录 预备工具 GNU make version 3.80 or newer; ...

  2. javascript加密之md5加密

    原作地址:JavaScriptMd5 修改备用:JavaScriptMd5.rar

  3. OpenFOAM——气泡上升

    计算域的顶部为大气,其余部分为壁面 流体的物性参数为: 首先进行建模操作,任何建模软件均可,本算例采用ICEM直接建模,生成网格,然后利用OpenFOAM下转化网格,划分完成的网格如下: 网格比较密集 ...

  4. 第07组 Beta冲刺(3/5)

    队名:摇光 队长:杨明哲 组长博客:求戳 作业博客:求再戳 队长:杨明哲 过去两天完成了哪些任务 文字/口头描述:代码编辑器,目前没什么进展 展示GitHub当日代码/文档签入记录:(组内共用,已询问 ...

  5. 为什么printf()用%f输出double型,而scanf却用%lf呢?

    转:https://blog.csdn.net/bat67/article/details/52056057 示例:double x:scanf(“%f”,&x):输入“123.4”,输出x的 ...

  6. 压测引起的 nginx报错 502 no live upstreams while connecting to upstream解决

    对系统的某个接口进行极限压测,随着并发量上升,nginx开始出现502 no live upstreams while connecting to upstream的报错,维持最大并发量一段时间,发现 ...

  7. [E2E_L9]Linux命令行上传文件到百度网盘

    百度有2TB 存储空间,在有第三方服务器的情况下,很多东西不需要下载到本地,可以直接使用服务转存,这非常好. 系统环境: Linux 系统 + Python 2.7 安装软件工具:[可能会要重复装] ...

  8. linux下node.js 查版本号和更新 how to update node

    我用的Mac,不是windows,不太清楚那个怎么搞. Linux下就是终端直接命令 //查版本号 node --version // v6.10.1 我很久没更了 //更新 //先清理Npm的cac ...

  9. Sword C语言原子操作

    /* gcc内置原子操作 */ #include <stdio.h> /* 知识补充: gcc 4.1.2版本之后,对X86或X86_64支持内置原子操作.即不需要引入第三方库(如pthr ...

  10. Angularjs 标签使用整理

    持续更新..... 一.select setmealList为接收到的集合数据,sname 是要显示的字段,Object属性 套餐类型:<select style="width: 15 ...