问题描述

给定一个大小为 n 的数组,找到其中的众数。众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素。

你可以假设数组是非空的,并且给定的数组总是存在众数。

示例 1:

输入: [3,2,3]
输出: 3

示例 2:

输入: [2,2,1,1,1,2,2]
输出: 2

解决方案

class Solution:
# two pass + dictionary
def majorityElement1(self, nums):
dic = {}
for num in nums:
dic[num] = dic.get(num, 0) + 1
for num in nums:
if dic[num] > len(nums)//2:
return num # one pass + dictionary
def majorityElement2(self, nums):
dic = {}
for num in nums:
if num not in dic:
dic[num] = 1
if dic[num] > len(nums)//2:
return num
else:
dic[num] += 1 # TLE
def majorityElement3(self, nums):
for i in range(len(nums)):
count = 0
for j in range(len(nums)):
if nums[j] == nums[i]:
count += 1
if count > len(nums)//2:
return nums[i] # Sotring
def majorityElement4(self, nums): return sorted(nums)[len(nums)//2] # Bit manipulation
def majorityElement5(self, nums):
bit = [0]*32
for num in nums:
for j in range(32):
bit[j] += num >> j & 1
res = 0
for i, val in enumerate(bit):
if val > len(nums)//2:
# if the 31th bit if 1,
# it means it's a negative number
if i == 31:
res = -((1 << 31)-res)
else:
res |= 1 << i
return res # Divide and Conquer
def majorityElement6(self, nums):
if not nums:
return None
if len(nums) == 1:
return nums[0]
a = self.majorityElement(nums[:len(nums)//2])
b = self.majorityElement(nums[len(nums)//2:])
if a == b:
return a
return [b, a][nums.count(a) > len(nums)//2] # the idea here is if a pair of elements from the
# list is not the same, then delete both, the last
# remaining element is the majority number
def majorityElement(self, nums):
count, cand = 0, 0
for num in nums:
if num == cand:
count += 1
elif count == 0:
cand, count = num, 1
else:
count -= 1
return cand

LeetCode(169. 求众数)的更多相关文章

  1. Java实现 Leetcode 169 求众数

    public static int majorityElement(int[] nums) { int num = nums[0], count = 1; for(int i=1;i<nums. ...

  2. 【Leetcode】【简单】【169求众数】【JavaScript】

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

  3. Leetcode之分治法专题-169. 求众数(Majority Element)

    Leetcode之分治法专题-169. 求众数(Majority Element) 给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是 ...

  4. Leetcode 229.求众数II

    求众数II 给定一个大小为 n 的数组,找出其中所有出现超过 ⌊ n/3 ⌋ 次的元素. 说明: 要求算法的时间复杂度为 O(n),空间复杂度为 O(1). 示例 1: 输入: [3,2,3] 输出: ...

  5. leetcode之求众数

    求众数 给定一个大小为 n 的数组,找到其中的众数. 你可以假设数组是非空的,并且给定的数组总是存在众数. 示例 1: 输入: [3,2,3] 输出: 3 示例 2: 输入: [2,2,1,1,1,2 ...

  6. 面试之leetcode分治-求众数,x幂等

    1 leetcode50 计算 x 的 n 次幂函数. 实现 pow(x, n) ,即计算 x 的 n 次幂函数. (1)调用库函数 (2)暴力o(N) (3)分治 xxxxxx.......x    ...

  7. Java实现 LeetCode 229 求众数 II(二)

    229. 求众数 II 给定一个大小为 n 的数组,找出其中所有出现超过 ⌊ n/3 ⌋ 次的元素. 说明: 要求算法的时间复杂度为 O(n),空间复杂度为 O(1). 示例 1: 输入: [3,2, ...

  8. 169.求众数 leetcode Javascript

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

  9. Leetcode题目169.求众数(简单)

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

随机推荐

  1. Datagrip导入导出为一个sql文件详细说明 (mysql)

    Datagrip导入导出为一个sql文件详细说明 导出: (我的无法截图, 借用一张) 导入: (我的无法截图, 再借用一张) 完

  2. jQuery示例

    <!DOCTYPE html><html lang="en" class="loading"><head> <meta ...

  3. Android相关 博客收藏

    #1 Android 网络编程 参考博客 :http://blog.csdn.net/kieven2008/article/details/8210737 #2 Could not find com. ...

  4. 提取Word里的文本内容 C#

    using DocumentFormat.OpenXml.Packaging; public static string TextFromWord(string path) { const strin ...

  5. 饮冰三年-人工智能-linux-04 vim编辑器

    vim的三种模式:命令行模式.编辑模式.扩展模式 1:命令行模式下常见的操作 删除 a):dd 删除光标所在当前行 b):ndd   删除光标所在当前行后的n行 复制 c):yy 复制光标所在当前行 ...

  6. The error may exist in com/bjpowernode/dao/StudentDao.xml ### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderExcept

    The error may exist in com/bjpowernode/dao/StudentDao.xml### Cause: org.apache.ibatis.builder.Builde ...

  7. java实现满天星swing&awt

    一起有两个类 1.MyStar.java package day02; import java.awt.Color; import javax.swing.JFrame;import javax.sw ...

  8. python 装饰器(1)

    # 装饰器,对一个函数打扮 # def foo():# print("foo")# foo# foo()# def test1():# print('-----1-----')# ...

  9. Vim设计

    像 IDE 一样使用 vim 免费的编程中文书籍索引

  10. org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorCode = ConnectionLoss for /eclipse20171118

    1:如果有一天,你有幸看到了这个错误,也许你像我一样low,因为此时,你已经准备开发Zookeeper程序了,却还没有把Zookeeper的服务启动起来. org.apache.zookeeper.K ...