47.Majority Element I & II
Majority Element I
描述
给定一个整型数组,找出主元素,它在数组中的出现次数严格大于数组元素个数的二分之一。
You may assume that the array is non-empty and the majority number always exist in the array.
样例
给出数组[1,1,1,1,2,2,2],返回 1
挑战
要求时间复杂度为O(n),空间复杂度为O(1)
class Solution:
"""
@param: nums: a list of integers
@return: find a majority number
"""
def majorityNumber(self, nums):
# write your code here
tmp = nums[0]
count = 1
for num in nums[1:]:
if count == 0 :
tmp = num
count = 1
if tmp == num :
count+=1
else:
count-=1
return tmp
Majority Element II
描述
给定一个整型数组,找到主元素,它在数组中的出现次数严格大于数组元素个数的三分之一。
数组中只有唯一的主元素
样例
给出数组[1,2,1,2,1,3,3] 返回 1
挑战
要求时间复杂度为O(n),空间复杂度为O(1)。
class Solution:
"""
@param: nums: a list of integers
@return: The majority number that occurs more than 1/3
"""
def majorityNumber(self, nums):
# write your code here
if not nums:
return []
count1, count2, candidate1, candidate2 = 0, 0, 0, 0
for n in nums:
if n == candidate1:
count1 += 1
elif n == candidate2:
count2 += 1
elif count1 == 0:
candidate1, count1 = n, 1
elif count2 == 0:
candidate2, count2 = n, 1
else:
count1, count2 = count1 - 1, count2 - 1
#return [n for n in (candidate1, candidate2) if nums.count(n) > len(nums) // 3]
if nums.count(candidate1) > len(nums) // 3 :
return candidate1
elif nums.count(candidate2) > len(nums) // 3 :
return candidate2
47.Majority Element I & II的更多相关文章
- LeetCode Majority Element I && II
原题链接在这里:Majority Element I,Majority Element II 对于Majority Element I 来说,有多重解法. Method 1:最容易想到的就是用Hash ...
- 47 Majority Element II
原题网址; https://www.lintcode.com/problem/majority-element-ii/ 描述 给定一个整型数组,找到主元素,它在数组中的出现次数严格大于数组元素个数的三 ...
- Majority Element I&II
题号:169, 229. 思路:当要找到超过n/c的元素x时,在更新candidates时,要保证,是x时加1,不是x时消掉c-1.则最终,x一定会出现在一个candidate中.
- LeetCode(169)Majority Element and Majority Element II
一个数组里有一个数重复了n/2多次,找到 思路:既然这个数重复了一半以上的长度,那么排序后,必然占据了 a[n/2]这个位置. class Solution { public: int majorit ...
- Majority Element,Majority Element II
一:Majority Element Given an array of size n, find the majority element. The majority element is the ...
- 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 ...
- Majority Element(169) && Majority Element II(229)
寻找多数元素这一问题主要运用了:Majority Vote Alogrithm(最大投票算法)1.Majority Element 1)description Given an array of si ...
- 【LeetCode】229. Majority Element II
Majority Element II Given an integer array of size n, find all elements that appear more than ⌊ n/3 ...
随机推荐
- P1184 高手之在一起(字典树模板题,hash算法, map)
哎,唯一值得说明的是,这道题的输入有bug 先把字典树的算法模板放一下 #include<iostream> #include<cstring> using namespace ...
- Python:Day48 Jquery
引用方式: <script src="jquery-3.3.1.js"></script> jQuery就是一个jQuery对象,可以简写成$ 基本语法:$ ...
- linux 应用和发展
课程大纲 UNIX/Linux发展历史 自由软件 Linux应用领域 Linux学习方法 UNIX 发展历史 (1 )1965年,美国麻省理工学院(MIT). 通用电气公司(G ...
- ROS 小乌龟测试
教程 1.维基 http://wiki.ros.org/cn/ROS/Tutorials 2. 创客智造 http://www.ncnynl.com/category/ros-junior-tutor ...
- >/dev/null 2>&1和2>&1 >/dev/null区别
>/dev/null 2>&1和2>&1 >/dev/null区别 >/dev/null 2>&1 //会将标准输出,错误输出都重定向至/d ...
- 多个窗口开启后,切换到指定title的窗口
1.在google中,可以开启多个窗口,这是需要切换到自己需要的窗口去定位元素.如下: #获取两个窗口的标题 ${titles} Selenium2Library.Get Window Titles ...
- Spring Security(二十二):6.4 Method Security
From version 2.0 onwards Spring Security has improved support substantially for adding security to y ...
- Docker安装MySQL并配置远程访问
1.docker search mysql 查看mysql版本 2.docker pull mysql 要选择starts最高的那个name 进行下载 3.docker images 查看下载好的镜像 ...
- 用for循环打印九九乘法表(for嵌套循环)
package com.Summer_0416.cn; /** * @author Summer * */ public class Test_Method10 { public static voi ...
- sublime text3 的汉化
仅是记录自己的处理过程,以防遗忘: 感谢作者:https://www.jianshu.com/p/ecc241f22ed5