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. 查看.NET应用程序中的异常(下)

    为什么要使用内存转储进行调试? 在两种主要情况下,您可能需要使用内存转储进行调试.第一种情况是应用程序有一个未处理的异常并崩溃,而您只有一个内存转储.第二种情况是,在生产环境中出现异常或特定行为,并且 ...

  2. nodejs之mysql查询

    示例代码中的mysql版本 2.14.1 参考代码 /** * 测试mysql连接 */ var mysql = require('mysql'); var connection = mysql.cr ...

  3. 玩家属性同步优化-脏数据标记(位运算、数组、stl之bitset)

    把大神的帖子中一部分摘抄出来,结合自己写的位运算代码和循环代码(数组遍历)进行性能测试分析并给出结果. 摘自: https://www.gameres.com/827195.html 本文适用于所有脏 ...

  4. docker不能正常使用cron任务

    基于centos7的docker不能正常使用cron任务问题 修改docker centos7镜像对应的容器的/etc/pam.d/crond文件 把required 修改成sufficient,如下 ...

  5. PhpStorm 设置自动FTP同步文件

    1.添加一个FTP服务器 ① 首先在这里打开添加FTP的页面,步骤,工具栏 -> Tools -> Deployment -> Configuration .     ②添加服务器  ...

  6. 第10组alpha冲刺(2/4)

    队名:凹凸曼 组长博客 作业博客 组员实践情况 童景霖 过去两天完成了哪些任务 文字/口头描述 继续学习Android studio和Java 完善项目APP原型 展示GitHub当日代码/文档签入记 ...

  7. Linux 文件压缩、打包

    文件压缩 计算机使用byte单位来计量.实际上,计算机最小的计量单位是bit.1byte = 8 bit.如果记录1这个数字,00000001,1会在最右边占一个1个bit 其他7个bit会被填上0. ...

  8. 刷题记录:[XNUCA2019Qualifier]EasyPHP

    目录 刷题记录:[XNUCA2019Qualifier]EasyPHP 解法一 1.error_log结合log_errors自定义错误日志 2.include_path设置包含路径 3.php_va ...

  9. 以SQL命令方式调用存储过程

    string str = "Data Source=.;Initial Catalog=***;Integrated Security=True"; using (SqlConne ...

  10. PostgreSQL体系架构与内存结构

    PostgreSQL体系架构 PostgreSQL的内存结构