leetcode 【 Majority Element 】python 实现

题目

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.

代码:oj测试通过 Runtime: 197 ms

 1 class Solution:
 2     # @param num, a list of integers
 3     # @return an integer
 4     def majorityElement(self, num):
 5         if len(num) == 1:
 6             return num[0]
 7
 8         candidate = 0
 9         count = 0
10         for i in range(len(num)):
11             if count == 0:
12                 candidate = num[i]
13                 count += 1
14             else:
15                 if num[i] == candidate :
16                     count += 1
17                 else:
18                     count -= 1
19         return candidate

思路

这个自己想不出来。上网找的Moore Voting算法。

这个方法在思路上还是比较朴实无华的,但是很精妙。

注意题目的条件,重复出现大于数据长度一半的元素为众数。

因此可以采用一种对冲思路:一旦相邻的两个元素不同,就把这两个元素对冲抵消掉;由于众数的出现频次大于数据其他所有元素出现频次之和,所以这种对冲抵消最后剩下的一定是众数。

之前看过几篇日志说这道题 还不错 记录在下面:

http://www.yanyulin.info/pages/2014/12/851338983752.html

http://www.geeksforgeeks.org/majority-element/

http://bookshadow.com/weblog/2014/12/22/leetcode-majority-element/

时间: 2024-10-22 07:15:31

leetcode 【 Majority Element 】python 实现的相关文章

leetcode Majority Element python

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. python code

LeetCode "Majority Element II"

Similar as "Majority Element". There are at most 2 such elements, for > floor(n/3), and every non-hit element will decrease count of EACH hit element. https://leetcode.com/discuss/42769/o-n-time-and-in-o-1-space-c-solution

[LeetCode]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. Credits: Special thanks to @

LeetCode——Majority Element

在一个数组中找到主要的元素,也就是出现次数大于数组长度一半的元素.容易想到的方式就是计数,出现次数最多的就是majority element,其次就是排序,中间的就是majority element.但是还有两种更有意思的实现方式时间效率O(n),空间效率O(1):1.Moore voting algorithm 投票算法,因为符合要求的majority element总是存在的,所以首先置计数器count=1,并选择数组的第一个元素作为candidate,往后遍历并计数,与candidate相

LeetCode—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. 其实就是找到序列中出现次数最多的元素,而且已经提出假设这

[LeetCode] 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. Credits:Special thanks to @t

leetcode Remove Element python

class Solution(object): def removeElement(self, nums, val): """ :type nums: List[int] :type val: int :rtype: int """ if len(nums) <= 0: return 0 k=0 for i in nums: if i != val: nums[k]=i k+=1 return k

LeetCode:Majority Element Ⅱ

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. 解法一: 1 class Solution { 2 public: 3 vector<int> majorityElement(vector<int>& nums) { 4 unor

【LeetCode 229】Majority Element II

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. 思路: [LeetCode 169]Majority Element 的拓展,这回要求的是出现次数超过三分之一次的数字咯,动动我们的大脑思考下,这样的数最多会存在几个呢,当然是2个嘛.因此,接着上一题的方