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的更多相关文章

  1. 169. Majority Element(C++)

    169. Majority Element Given an array of size n, find the majority element. The majority element is t ...

  2. 23. leetcode 169. Majority Element

    169. Majority Element Given an array of size n, find the majority element. The majority element is t ...

  3. 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 ...

  4. leetcode 169. Majority Element 、229. Majority Element II

    169. Majority Element 求超过数组个数一半的数 可以使用hash解决,时间复杂度为O(n),但空间复杂度也为O(n) class Solution { public: int ma ...

  5. Leetcode#169. Majority Element(求众数)

    题目描述 给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. 示例 1: 输入: [3,2,3] ...

  6. Week1 - 169.Majority Element

    这周刚开始讲了一点Divide-and-Conquer的算法,于是这周的作业就选择在LeetCode上找分治法相关的题目来做. 169.Majority Element Given an array ...

  7. 169. Majority Element - LeetCode

    Question 169. Majority Element Solution 思路:构造一个map存储每个数字出现的次数,然后遍历map返回出现次数大于数组一半的数字. 还有一种思路是:对这个数组排 ...

  8. 【LeetCode】169. Majority Element 解题报告(Java & Python & C+)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 思路 hashmap统计次数 摩尔投票法 Moore ...

  9. 【leetcode❤python】169. Majority Element

    #Method 1import math class Solution(object):    def majorityElement(self, nums):        numsDic={}   ...

随机推荐

  1. git apply failed (转载)

    转自:http://blog.csdn.net/aaronzzq/article/details/6955893 git version 1.6.0.4 几个新手刚刚开始接触 Git,为了维护核心仓库 ...

  2. (水题)洛谷 - P1014 - Cantor表

    https://www.luogu.org/problemnew/show/P1014 很显然同一对角线的和是相等的.我们求出前缀和然后二分. 最后注意奇偶的顺序是相反的. #include<b ...

  3. hdoj5563(简单几何)

    笑死我了,居然一遍就过了. 题意:给你5个点,让你判断能否组成一个五角星: 思路: 利用对称性,应该有很多种方法.弱打的很麻烦.但是那个两点式公式去判断另外一个点是否在一条直线上的那个判断,还是蛮喜欢 ...

  4. IT兄弟连 JavaWeb教程 HTTP协议

    超文本传输协议(HTTP,Hypertext Transfer Protocol)是互联网上应用最为广泛的一种网络协议.所有的Web文件都必须遵守这个标准.设计HTTP最初的目的是为了提供一种发布和接 ...

  5. 数论/暴力 Codeforces Round #305 (Div. 2) C. Mike and Frog

    题目传送门 /* 数论/暴力:找出第一次到a1,a2的次数,再找到完整周期p1,p2,然后以2*m为范围 t1,t2为各自起点开始“赛跑”,谁落后谁加一个周期,等到t1 == t2结束 详细解释:ht ...

  6. 字符串处理 Codeforces Round #305 (Div. 2) A. Mike and Fax

    题目传送门 /* 字符串处理:回文串是串联的,一个一个判断 */ #include <cstdio> #include <cstring> #include <iostr ...

  7. 微信小程序消息推送,前端操作

    <form bindsubmit="getFormId" report-submit="true"> <button form-type=&q ...

  8. What's mean ORA-25191?

    1.在给表授权的时候.报错ORA-25191 检查该表为IOT 表 . --因为不是按照单表方式授予权限,而是按照用户的方式授予权限,所以该表的父亲表都在该用户下,所以这个报错可以忽略. 2/ 在参考 ...

  9. 基于CentOS 7.2个人网盘的实现

    首先使用YUM安装依赖环境: [root@sishen ~]#yum install python python-setuptools python-imaging python-ldap pytho ...

  10. C++ thread operator= 右值引用 vector foreach

    这是 thread 的construct定义: default (1) thread() noexcept; initialization (2) template <class Fn, cla ...