169. Majority Element
2015-12-23 11:32
288 查看
Given an array of size n, find the majority element. The majority element is the element that appears more than
You may assume that the array is non-empty and the majority element always exist in the array.
class Solution(object):
def majorityElement(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
a=0
b=0
for i in nums:
if b==0:
a=i
b=1
else:
if a==i:
b+=1
else:
b-=1
return a
⌊ n/2 ⌋times.
You may assume that the array is non-empty and the majority element always exist in the array.
class Solution(object):
def majorityElement(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
a=0
b=0
for i in nums:
if b==0:
a=i
b=1
else:
if a==i:
b+=1
else:
b-=1
return a
相关文章推荐
- FFMPEG中常用的几种结构体
- 深入浅出HTTP协议(WEB开发和面试必备)
- Linux下针对路由功能配置iptables的方法详解
- JavaScript
- ElasticSearch学习7_elasticsearch配置小记
- 不常用的日期:NSCalendar + NSDateComponents
- 如何使用openssl生成RSA公钥和私钥对
- 获取用户真实的IP
- Leetcode: Palindrome Permutation
- c++中string类型用下标初始化后str.size()为0 输出string值为空
- ISO7816智能卡读写源代码
- android studio 批量打包
- 单链表代码1
- M3U8文件真实TS流获取工具
- 在iOS 8中使用UIAlertController
- 日经春秋 20151223
- 【转载】理解VB中选择语句Select Case结构的最好例子
- iOS 在UILabel显示不同的字体和颜色
- Linux Kernel devm_* API源码分析
- 执行计划解读 简朝阳 (Sky Jian) and 那蓝蓝海