[leetcode: Python]169. Majority Element
来源:互联网 发布:js怎么给div赋值 编辑:程序博客网 时间:2024/05/21 03:16
题目:
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.
题意:
给定一个数组,找到它的主元素,即出现次数超过n/2的数。
假定数组非空且数组存在主元素。
方法一:性能66ms
class Solution(object): def majorityElement(self, nums): """ :type nums: List[int] :rtype: int """ b = {} for eachnum in nums: if eachnum not in b: b[eachnum] = 1 else: b[eachnum] += 1 for eachnum in b: if b[eachnum] == max(b.values()): return eachnum return 0
方法二:性能49ms
class Solution(object): def majorityElement(self, nums): """ :type nums: List[int] :rtype: int """ return sorted(nums)[(len(nums))/2]
0 0
- leetcode 169. Majority Element python
- [leetcode: Python]169. Majority Element
- leetcode python 169. Majority Element
- LeetCode 169. Majority Element(Python)
- [LeetCode]169.Majority Element
- LeetCode 169.Majority Element
- 【LeetCode】169.Majority Element
- LeetCode 169. Majority Element
- LeetCode 169. Majority Element
- [leetcode] 169.Majority Element
- Leetcode 169. Majority Element
- 【LeetCode】169. Majority Element
- [LeetCode]169. Majority Element
- 169. Majority Element LeetCode
- LeetCode 169. Majority Element
- LeetCode 169. Majority Element
- Leetcode 169. Majority Element
- [LeetCode]169. Majority Element
- Android开发者,必须知道的并且没有遇到的异常,附带简单的解决方案和异常过程、源码的分析从java层到Jni层
- 转码和解码
- VGG
- vps 首选 kdatacenter
- == 与 equals 的恩怨情仇
- [leetcode: Python]169. Majority Element
- 计算机视觉——问题
- AndroidStudio 在导航栏 设置小扳手图标
- java对象 json对象 互转jar包
- 工作流管理--模型、方法和系统
- JS判断浏览器类型
- 解决eclipse报PermGen space异常的问题
- hadoop 默认端口说明
- Map 综述(二):彻头彻尾理解 LinkedHashMap