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

  1. LeetCode Majority Element I && II

    原题链接在这里:Majority Element I,Majority Element II 对于Majority Element I 来说,有多重解法. Method 1:最容易想到的就是用Hash ...

  2. 47 Majority Element II

    原题网址; https://www.lintcode.com/problem/majority-element-ii/ 描述 给定一个整型数组,找到主元素,它在数组中的出现次数严格大于数组元素个数的三 ...

  3. Majority Element I&II

    题号:169, 229. 思路:当要找到超过n/c的元素x时,在更新candidates时,要保证,是x时加1,不是x时消掉c-1.则最终,x一定会出现在一个candidate中.

  4. LeetCode(169)Majority Element and Majority Element II

    一个数组里有一个数重复了n/2多次,找到 思路:既然这个数重复了一半以上的长度,那么排序后,必然占据了 a[n/2]这个位置. class Solution { public: int majorit ...

  5. Majority Element,Majority Element II

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

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

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

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

  8. Majority Element(169) && Majority Element II(229)

    寻找多数元素这一问题主要运用了:Majority Vote Alogrithm(最大投票算法)1.Majority Element 1)description Given an array of si ...

  9. 【LeetCode】229. Majority Element II

    Majority Element II Given an integer array of size n, find all elements that appear more than ⌊ n/3 ...

随机推荐

  1. 创建 tomcat 服务的镜像

    如何设计 Tomcat 的 Dockerfile $ sudo docker search tomcat |wc -l 285 在 dockerhub 上搜索与 tomcat 相关的镜像,有如此之多的 ...

  2. Java面试知识点之线程篇(二)

    前言:接上篇,这里继续对java线程相关知识点进行总结. 1.notify和notifyall的区别 notify()方法能够唤醒一个正在等待该对象的monitor的线程,当有多个线程都在等待该对象的 ...

  3. flask的migrate

    https://blog.csdn.net/kevin_qq/article/details/51777190 这个方法可以: https://www.cnblogs.com/caicairui/p/ ...

  4. 用deepin堆砌工作环境

    用deepin堆砌工作环境 这篇文章记录了我用 deepin 15.5搭建工作环境的过程,供我个人在未来重装系统时参考.对于其他以 deepin 操作系统作为主要工作平台的看官,咱们是相亲相爱的一家人 ...

  5. Linux三剑客-AWK

    1.什么是awk AWK是一种处理文本文件的语言,是一个强大的文本分析工具.有统计和计算功能. 之所以叫AWK是因为其取了三位创始人 Alfred Aho,Peter Weinberger, 和 Br ...

  6. nginx-redirect配置

    转载一篇非常好的文章,大赞!!!!! http://blog.csdn.net/u010391029/article/details/50395680 nginx的配置文件解读 http://blog ...

  7. y7000笔记本 darknet-yolo安装与测试(Ubuntu18.04+Cuda9.0+Cudnn7.1)

    环境配置看上一贴 https://www.cnblogs.com/clemente/p/10386479.html 1 安装darknet 1-1 克隆darknet repo git clone h ...

  8. ogg BR – BOUNDED RECOVERY

    BR – BOUNDED RECOVERY 适用于 Extract 进程(仅适用于 Oracle数据库) 使用 BR 参数可以控制 GoldenGate 的 Bounded Recovery (BR) ...

  9. Android测试(一):在Android中测试App

    原文:https://developer.android.com/training/testing/index.html 测试你的App是开发过程中的重要组成部分.通过对应用程序持续的运行测试,你可以 ...

  10. Spring整合ActiveMQ及多个Queue消息监听的配置

        消息队列(MQ)越来越火,在java开发的项目也属于比较常见的技术,MQ的相关使用也成java开发人员必备的技能.笔者公司采用的MQ是ActiveMQ,且消息都是用的点对点的模式.本文记录了实 ...