169. 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.
原题地址: Majority Element
难度: Easy
题意: 找出数量超过数组长度一半的值
思路1:
对数组进行排序, 由于某个值的数量超过数组长度的一半,排序之后,数组中间的值必然是要求的结果
代码:
class Solution(object):
def majorityElement(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
nums.sort()
return nums[len(nums)/2]
时间复杂度: O(nlog(n)),即排序的复杂度
空间复杂度: O(1)
思路2:
遍历数组,进行统计
class Solution(object):
def majorityElement(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
d = {}
for num in nums:
d[num] = d.get(num, 0) + 1
if d[num] > len(nums) / 2:
return num
时间复杂度: O(n)
空间复杂度: O(n)
思路3:
摩尔投票法: 将数组中不同两个数配成对
代码:
class Solution(object):
def majorityElement(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
n = len(nums)
count = 1
num = nums[0]
for i in range(1, n):
if nums[i] == num:
count += 1
elif count == 0:
num = nums[i]
count += 1
else:
count -= 1 count = 0
for i in range(n):
if nums[i] == num:
count += 1 if count > n / 2:
return num
时间复杂度: O(n)
空间复杂度: O(1)
169. Majority Element@python的更多相关文章
- 169. Majority Element(C++)
169. Majority Element Given an array of size n, find the majority element. The majority element is t ...
- 23. leetcode 169. Majority Element
169. Majority Element Given an array of size n, find the majority element. The majority element is t ...
- LeetCode Javascript实现 169. Majority Element 217. Contains Duplicate(两个对象比较是否相等时,如果都指向同一个对象,a==b才是true)350. Intersection of Two Arrays II
169. Majority Element /** * @param {number[]} nums * @return {number} */ var majorityElement = funct ...
- leetcode 169. Majority Element 、229. Majority Element II
169. Majority Element 求超过数组个数一半的数 可以使用hash解决,时间复杂度为O(n),但空间复杂度也为O(n) class Solution { public: int ma ...
- Leetcode#169. Majority Element(求众数)
题目描述 给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. 示例 1: 输入: [3,2,3] ...
- Week1 - 169.Majority Element
这周刚开始讲了一点Divide-and-Conquer的算法,于是这周的作业就选择在LeetCode上找分治法相关的题目来做. 169.Majority Element Given an array ...
- 169. Majority Element - LeetCode
Question 169. Majority Element Solution 思路:构造一个map存储每个数字出现的次数,然后遍历map返回出现次数大于数组一半的数字. 还有一种思路是:对这个数组排 ...
- 【LeetCode】169. Majority Element 解题报告(Java & Python & C+)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 思路 hashmap统计次数 摩尔投票法 Moore ...
- 【leetcode❤python】169. Majority Element
#Method 1import math class Solution(object): def majorityElement(self, nums): numsDic={} ...
随机推荐
- hexo博客实现多终端共享&webhook自动化部署
摘要:好不容易搭建了hexo,还不满足.想要实现在小程序上也能访问博客,又不想再写一个后台.每次更新文章到服务器之后,希望能自动同步到网站上面.如果你有这样的需求,那么希望这篇文章能帮助到你. 我的配 ...
- 洛谷 - P1987 - 摇钱树 - dp - 贪心
https://www.luogu.org/problemnew/show/P1987 这道题,假如是n==k,也就是把所有的树都砍完,我就知道要贪心去做,因为树给的初始金币是固定的,每天掉金币,当然 ...
- hdoj1166【线段树】
单点更新+区间求和 不多说,直接上渣code--- #include<cstdio> #include<iostream> #include<string.h> # ...
- bzoj 1023: [SHOI2008]cactus仙人掌图【tarjan+dp+单调队列】
本来想先求出点双再一个一个处理结果写了很长发现太麻烦 设f[u]为u点向下的最长链 就是再tarjan的过程中,先照常处理,用最长儿子链和次长儿子链更新按ans,然后处理以这个点为根的环,也就是这个点 ...
- 最短路之Floyd(弗洛伊德)
只有五行的Floyd最短路算法: 核心代码 每次都更新通过k点,然后从i到j的最短路程...
- iOS NavigationBar 导航栏自定义
1. 设置导航栏NavigationBar的背景颜色: a) setBarTintColor : 设置NagivationBar的颜色 也可以用 : [[UINavigationBar appear ...
- UvaLive3942(Trie + dp)
查了半天数组越界的RE,才发现自己把ch数组放结构体里是过大的……放全局就A了. 类似区间的dp比较显然,只是用trie树做了优化,使得可以在trie树里一边走一边往上加dp值,不必枚举以前的每个位置 ...
- ubuntu14.04 在Dash中添加条目并把它放到启动器上
1. 创建studio.desktop,内容如下:注意路径. [Desktop Entry] Version=2.2.3 Name=Android Studio Exec=/home/你的用户名/an ...
- 486 Predict the Winner 预测赢家
给定一个表示分数的非负整数数组. 玩家1从数组任意一端拿取一个分数,随后玩家2继续从剩余数组任意一端拿取分数,然后玩家1拿,…….每次一个玩家只能拿取一个分数,分数被拿取之后不再可取.直到没有剩余分数 ...
- Hadoop体系结构管理
一.查看Zookeeper信息 [hadoop@weekend01 ~]$zkServer.sh status JMX enabled by default Using config: /hadoop ...