LeetCode Majority Element(简单题)
题意:
给一个数组,其中有一个元素的出现次数已经超过数组的一半大小,请找出这个元素?
思路:
可以扫一遍数组,将这个出现次数过多的元素抵消其他的元素,最后必定留下1个以上的元素,就是它自己了。
python3
扫一遍
class Solution(object):
def majorityElement(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
idx,cnt,cur=0,0,0
while idx<len(nums):
idx+=1
if cnt==0:
cnt=1
cur=nums[idx-1]
continue
if nums[idx-1]!=cur:
cnt-=1
else:
cnt+=1
return cur
AC代码
排序
class Solution(object):
def majorityElement(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
return sorted(nums)[int(len(nums)/2)]
AC代码
dict
class Solution(object):
def majorityElement(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
dic={}
for x in nums:
dic[x]=dic.get(x,0)+1
if dic[x]*2>len(nums):
return x
AC代码
LeetCode Majority Element(简单题)的更多相关文章
- 2016.5.18——leetcode:Majority Element
Majority Element 本题收获: 1.初步了解hash,nth_element的用法 2.题目的常规思路 题目: Given an array of size n, find the ma ...
- [LeetCode] Majority Element II 求众数之二
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...
- LeetCode Majority Element I && II
原题链接在这里:Majority Element I,Majority Element II 对于Majority Element I 来说,有多重解法. Method 1:最容易想到的就是用Hash ...
- [LeetCode] Majority Element II 求大多数之二
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. Note: The a ...
- LeetCode Majority Element I
原题链接在这里:https://leetcode.com/problems/majority-element/ 题目: Given an array of size n, find the major ...
- [LeetCode] Majority Element 求众数
Given an array of size n, find the majority element. The majority element is the element that appear ...
- [LeetCode] Majority Element 求大多数
Given an array of size n, find the majority element. The majority element is the element that appear ...
- LeetCode——Majority Element
在一个数组中找到主要的元素,也就是出现次数大于数组长度一半的元素.容易想到的方式就是计数,出现次数最多的就是majority element,其次就是排序,中间的就是majority element. ...
- LeetCode Majority Element Python
Given an array of size n, find the majority element. The majority element is the element that appear ...
随机推荐
- H5网页动画制作(页面切换、效果等)
网址 http://www.ih5.cn/#mine/home/194080 13482289820 视频教程 http://file.ih5.cn/?nid=713629&key=aef10 ...
- EF Code First:实体映射,数据迁移,重构(1)
一.前言 经过EF的<第一篇>,我们已经把数据访问层基本搭建起来了,但并没有涉及实体关系.实体关系对于一个数据库系统来说至关重要,而且EF的各个实体之间的联系,实体之间的协作,联合查询等也 ...
- Cookie实例,理解cookie
一.一句话了解cookie是什么 cookie是服务端发送给客户端的.用来记录一些信息(如用户名),定制主页,聚焦广告的.最终以文件形式存在于客户端电脑磁盘下的小型文档. 二.用实例来认清cookie ...
- 让Windows下的Tomcat将控制台信息记录到日志
在开发的过程中经常出现包冲突,却不知道怎么回事,可以在 catalina.bat 里面设置查看class加载日志 set CATALINA_OPTS=-server -Xdebug -Xnoage ...
- Javascript arguments详解
今天我们来看看arguments对象及属性.arguments对象不能显式创建,arguments对象只有函数开始时才可用.函数的 arguments 对象并不是一个数组,访问单个参数的方式与访问数组 ...
- Codeforces Round #378 (Div. 2) D题(data structure)解题报告
题目地址 先简单的总结一下这次CF,前两道题非常的水,可是第一题又是因为自己想的不够周到而被Hack了一次(或许也应该感谢这个hack我的人,使我没有最后在赛后测试中WA).做到C题时看到题目情况非常 ...
- Linux-守护进程的实现
Some basic rules to coding a daemon prevent unwanted interactions from happening. We state these rul ...
- [转]source inslght使用指导
作为一个开放源代码的操作系统,Linux附带的源代码库使得广大爱好者有了一个广泛学习.深入钻研的机会,特别是Linux内核的组织极为复杂,同时,又不能像windows平台的程序一样,可以使用集成开发环 ...
- 10 件在 PHP 7 中不要做的事情
1. 不要使用mysql_函数 这一天终于来了,从此你不仅仅"不应该"使用mysql_函数.PHP 7 已经把它们从核心中全部移除了,也就是说你需要迁移到好得多的mysqli_函数 ...
- Non-Programmer's Tutorial for Python 3/File IO
File I/O Here is a simple example of file I/O (input/output): # Write a file with open("test.tx ...