LeetCode算法练习Majority Element
来源:互联网 发布:windows多进程编程 编辑:程序博客网 时间:2024/06/07 13:52
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.
大致意思是找到数组中出现次数在超过50%的元素,在实际中统计学里会用到很多。这个问题我的第一思路是想到了n/2这个关键点的信息,如果想要一个元素出现次数超过n/2,那么在有序的前提下,第n/2元素必为该元素。
public int majorityElement(int[] num) {int len = num.length;List<Integer> list = new ArrayList<Integer>();for (int i : num) {list.add(i);}Collections.sort(list);int temp = list.get((list.size()-1)/2);int count = 1;for (Integer integer : list) {if(temp == integer){count++;}}return count>len/2?temp:0;
}
0 0
- LeetCode算法练习Majority Element
- LeetCode练习 | Majority Element
- LeetCode--Majority Element II & 多数投票算法
- LeetCode--Majority Element II & 多数投票算法
- 算法设计Week2 LeetCode #169 Majority Element
- 【算法作业3】LeetCode 169. Majority Element
- Leetcode算法学习日志-169 Majority Element
- LeetCode:Majority Element I II 投票算法
- Leetcode练习笔记——169. Majority Element
- 【leetcode】Majority Element II,Majority element algorithm
- Leetcode: Majority Element &Majority Element II
- Leetcode Majority Element
- leetcode 169-Majority Element
- 【leetcode 哈希表】Majority Element
- LeetCode:Majority Element
- leetcode:Majority Element
- leetcode: Majority Element
- [LeetCode] Majority Element
- 第一次
- BZOJ 3809 Gty的二逼妹子序列 莫队算法+分块
- JavaSE学习之前知识
- Java堆.栈和常量池
- LCOJ - Majority Element
- LeetCode算法练习Majority Element
- DOM解析XML格式数据
- 读取xls和xlsx
- Android入门笔记 - 网络通信 - WebKit
- sgu178:Chain(数学题)
- Curses library not found. Please install appropriate package
- android中的ellipsize
- 一条不知通向代码的道路
- OpenGL ES 着色器(shader)介绍