class Solution(object): def majorityElement(self, nums): """ :type nums: List[int] :rtype: int """ # cnts = {} # for num in nums: # if num in cnts: # cnts[num] += 1 # else: # cnts[num] = 1 # if cnts[num]> len(nums)/2: # return num vote = 0 majorNum = nums[0] for num in nums: if vote>0: if num == majorNum: vote = vote + 1 else: vote = vote - 1 if vote == 0: majorNum = num vote = 1 return majorNum test =Solution() nums1 = [1] print test.majorityElement(nums1)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question