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. 关于 GraPhlAn 的孤独自学

    最近需要用 GraPhlan 来绘制 taxo分类图,稍微研究了一下 一.简介 官网: http://huttenhower.sph.harvard.edu/GraPhlAn 主要有两个脚本: gra ...

  2. NOJ2026:Keroro侵略地球(01字典树)

    传送门 题意 略 分析 将n个数插入字典树中,m次查询,取最大值,复杂度\(O(mlogn)\) trick 1.注意题目给的空间,开40刚刚够(62852K) 2.作为01字典树的模板保存了 代码 ...

  3. 51nod 1050【DP】

    思路: 就是先正常的dp一下求一个最大连续子串,然后特殊情况就是sum-最小连续子串.. 比一比谁大谁小就好了 #include <stdio.h> #include <string ...

  4. 1.基础数据类型的初识 字符串 bool 整型 if else elif

    ---恢复内容开始--- 计算器基础知识 cpu :人类的大脑 运算和处理问题 内存:临时存储数据 断点就消失了 高铁 硬盘:永久存储数据 图片 操作系统:是一个软件 控制每个硬件之间的数据交互 Py ...

  5. Java 反射机制详解(下)

    续:Java 反射机制详解(上) 三.怎么使用反射 想要使用反射机制,就必须要先获取到该类的字节码文件对象(.class),通过字节码文件对象,就能够通过该类中的方法获取到我们想要的所有信息(方法,属 ...

  6. python之迷宫BFS

    # @File: maze_queue_bfs from collections import deque maze = [ [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 0 ...

  7. 第03课 在VMwave 14.0 上配置企业级CentOS 6.6操作系统

    第一部分:配置虚拟硬件 1.1 启动VMware,选择文件-->新建虚拟机(Ctrl + N),创建一个虚拟机. (VMware的安装过程较为简单,可自行百度.) 1.2 此时,出现新建虚拟机向 ...

  8. 记录Jmeter集成Jenkins运行Ant做接口监听

    最近在鼓捣Jmeter的接口测试,把他集成到了Jenkins上做自动化接口监听.把操作记录下来. 首先就是进行接口测试的编写.打开Jmeter.主要是把接口的测试逻辑和断言处理调通后就OK了,接口程序 ...

  9. 当不知道基本数据类型的取值范围时,可以通过max_value等来查询

    public class Demo03{ public static void main(String[] args){ System.out.println("int MAX " ...

  10. .net core区域设置方式

    在Startup中配置 默认路由是这个,并不包含区域路由 routes.MapRoute( name: "default", template: "{controller ...