LeetCode #229: Majority Element II
2016-09-22 11:20
309 查看
Problem Statement
(Source) 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.
Solution
This problems can be solved using a modified version of theBoyer–Moore majority vote algorithm(wiki).
There could be at most 2 elements that appear more than
⌊ n/3 ⌋times in an given array of size n. Hence the idea is to keep two possible candidates at the same time, and do a “appearance times check” to see if any of the possible candidates has appearance times more than
⌊ n/3 ⌋after the main loop.
相关文章推荐
- Majority Element II
- LeetCode Majority Element II
- LeetCode Majority Element II
- Majority Element II
- leetcode 229:Majority Element II
- leetcode之Majority Element II
- 229. Majority Element II
- Majority Element II
- [LeetCode]229. Majority Element II
- 229. Majority Element II
- Majority Element II
- LeetCode之Majority Element II
- 229. Majority Element II
- leetcode_c++:Majority Element II (229)
- [LeetCode]229. Majority Element II
- 229. Majority Element II
- 229. Majority Element II
- [leetcode] 229. Majority Element II
- Majority Element II
- leetcode 229[medium]--- Majority Element II