Leetcode229 Majority Element II
来源:互联网 发布:array push 二维数组 编辑:程序博客网 时间:2024/06/15 04:39
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋
times. The algorithm should run in linear time and in O(1) space.
public class Solution229 { public List<Integer> majorityElement(int[] nums) { List<Integer> result = new ArrayList<Integer>(); Map<Integer,Integer> map = new HashMap<Integer,Integer>(); for (int i = 0; i < nums.length;i++) { int cnt = 0; if(map.containsKey(nums[i])) { cnt = map.get(nums[i]).intValue()+1; } else { cnt=1; } map.put(nums[i],cnt); } int n = nums.length/3; for (Integer key:map.keySet()) { int cnt = map.get(key); if(cnt > n) { result.add(key); } } return result; } public static void main(String[] args) { }}
0 0
- LeetCode229:Majority Element II
- Leetcode229 Majority Element II
- leetcode229. Majority Element II
- leetcode229. Majority Element II
- leetcode229-Majority Element II(数组中出现最多的数II)
- Majority Element && Majority Element II
- Majority Element II
- [LeetCode] Majority Element II
- 229.Majority Element II
- Majority Element II
- [leetcode] Majority Element II
- Majority Element II
- #leetcode#Majority Element II
- 【LeetCode】Majority Element II
- LeetCode Majority Element II
- LeetCode - Majority Element II
- Majority Element II
- LeetCode Majority Element II
- hdu 1551 Cable master(二分法)
- KVC的步骤 以及重点
- UC/OS II 任务管理(2)之就绪表和就绪组
- Codeforces Gym 100340C ePig 模拟
- 登陆界面的数据处理(一)
- Leetcode229 Majority Element II
- expect
- c#使用itextsharp输出pdf(动态填充表单内容,显示中文)
- mysql 事务 与 数据库引擎
- 域传送漏洞总结
- Java中获取键盘输入值的三种方法
- 第0课第1节 刚接触开发板之接口接线工具
- Unity热更新专题(五)如何打包AssetBundle
- PhotoView源码分析(2)