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={} ...
随机推荐
- hdoj5327【前缀和思想】
题意: 找给定区间的美丽数,美丽数的意思就是这个数每个位上的数都是唯一的. 思路: 前缀和的思想. 感想: 就是你当前位置代表某个特性的前面的所有和(瞎比比的,说了下感觉).前提是你必须找到这样的特性 ...
- POJ1270【拓扑排序+DFS】
题意: 先给你一个字符串,让你给他们排序: 再给你一行,在这一行,每两个就是第一个需要在第二个前面: 思路: //DFS写多了感觉好有啊,就是排序过程中可能会有多种情况. //我们考虑一下怎么排好一个 ...
- poj 2891 Strange Way to Express Integers【扩展中国剩余定理】
扩展中国剩余定理板子 #include<iostream> #include<cstdio> using namespace std; const int N=100005; ...
- autolayout UIImageView 根据 UILabel的宽度变换位置
仅个人学习笔记,大牛勿喷 代码写法 使用Masonry //昵称 _nameLableView = [[UILabel alloc]init]; [_nameLableView setTextColo ...
- hdu1151 Air Raid 基础匈牙利
#include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> ...
- Hexo瞎折腾系列(3) - 添加GitHub彩带和GitHub Corner
页面右上角添加GitHub彩带 你可以在这里找到一共12种样式的GitHub彩带,复制其中的超链代码. 在themes\next\layout\_layout.swig目录下找到头部彩带相关的代码: ...
- python高级函数
1 函数 1.1 函数即变量 函数定义:把一个函数体作为变量赋值给一个函数名,同时函数体存放到内存中. 函数调用:根据函数名去内存中寻找对用的函数体,找到了就执行. >>> def ...
- ie下,php HTTP_REFERER获取失败的整理
HTTP_REFERER有效的情况1.以iframe 形式调用地址2.以window.open调用,打开新页面window.open(url);3.使用window.location.replace在 ...
- h5-18-文件上传
参考博客地址:https://developer.mozilla.org/zh-CN/docs/Web/API/FormData/Using_FormData_Objects 参考博客地址:http: ...
- HAL之定时器
一首先得对定时器的时钟与系统时钟的关系搞清楚,基本定时器的时钟来自APB1最大36MHZ.(定时器倍频值为1) 二 在STM32CubeMX中打开外设功能,时钟源选择内部:然后在配置定时器3中的参数设 ...