[Swift]LeetCode229. 求众数 II | Majority Element II
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10204770.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋
times.
Note: The algorithm should run in linear time and in O(1) space.
Example 1:
Input: [3,2,3] Output: [3]
Example 2:
Input: [1,1,1,3,3,2,2,2] Output: [1,2]
给定一个大小为 n 的数组,找出其中所有出现超过 ⌊ n/3 ⌋
次的元素。
说明: 要求算法的时间复杂度为 O(n),空间复杂度为 O(1)。
示例 1:
输入: [3,2,3] 输出: [3]
示例 2:
输入: [1,1,1,3,3,2,2,2] 输出: [1,2]
96ms
1 class Solution { 2 func majorityElement(_ nums: [Int]) -> [Int] { 3 guard nums.count > 1 else { 4 return nums 5 } 6 7 var majorA = nums[0] 8 var countA = 0 9 10 var majorB = nums[0] 11 var countB = 0 12 13 for index in 0...(nums.count-1) { 14 if nums[index] == majorA { 15 countA += 1 16 continue 17 } 18 19 if nums[index] == majorB { 20 countB += 1 21 continue 22 } 23 24 if countA == 0 { 25 majorA = nums[index] 26 countA += 1 27 continue 28 } 29 30 if countB == 0 { 31 majorB = nums[index] 32 countB += 1 33 continue 34 } 35 36 countA -= 1 37 countB -= 1 38 } 39 40 countA = 0 41 countB = 0 42 for index in 0...(nums.count - 1) { 43 if nums[index] == majorA { 44 countA += 1 45 } else if nums[index] == majorB { 46 countB += 1 47 } 48 } 49 50 var result = [Int]() 51 if countA > nums.count/3 { 52 result.append(majorA) 53 } 54 if countB > nums.count/3 { 55 result.append(majorB) 56 } 57 58 return result 59 } 60 }
104ms
1 class Solution { 2 func majorityElement(_ nums: [Int]) -> [Int] { 3 4 var res = [Int]() 5 var cm = 0, cn = 0, m = 0, n = 0 6 for a in nums { 7 if a == m { cm += 1 } 8 else if a == n { cn += 1 } 9 else if cm == 0 { m = a; cm = 1} 10 else if cn == 0 { n = a; cn = 1 } 11 else { cm -= 1; cn -= 1} 12 } 13 cm = 0; cn = 0 14 for a in nums { 15 if a == m { cm += 1 } 16 else if a == n { cn += 1 } 17 } 18 if cm > nums.count / 3 {res.append(m)} 19 if cn > nums.count / 3 {res.append(n)} 20 return res 21 } 22 }
112ms
1 class Solution { 2 func majorityElement(_ nums: [Int]) -> [Int] { 3 var elems: [Int: Int] = [:] 4 for n in nums { 5 elems[n, default: 0] += 1 6 } 7 return elems.compactMap { key, value in 8 if value > nums.count / 3 { return key } 9 return nil 10 } 11 } 12 }
112ms
1 class Solution { 2 func majorityElement(_ nums: [Int]) -> [Int] { 3 var dict:[Int : Int] = [Int:Int]() 4 for num in nums { 5 if let n = dict[num] { 6 dict[num] = n + 1 7 } else { 8 dict[num] = 1 9 } 10 } 11 var r = [Int]() 12 let m = nums.count / 3 13 for (k,v) in dict { 14 if v > m { 15 r.append(k) 16 } 17 } 18 return r 19 } 20 }
116ms
1 class Solution { 2 func majorityElement(_ nums: [Int]) -> [Int] { 3 4 if nums.count == 0 { 5 return [] 6 } 7 8 var dict: [Int: Int] = [:] 9 var resultList: Set<Int> = [] 10 let limit = nums.count/3 11 12 for number in nums { 13 var result = 1 14 if let temp = dict[number] { 15 result = temp + 1 16 } 17 dict[number] = result 18 19 if result > limit { 20 resultList.insert(number) 21 } 22 } 23 24 return Array(resultList) 25 } 26 }
124ms
1 class Solution { 2 func majorityElement(_ nums: [Int]) -> [Int] { 3 4 if nums.count == 0 { 5 return [] 6 } 7 8 var dict: [Int: Int] = [:] 9 var resultList: [Int] = [] 10 let limit = nums.count/3 11 12 for number in nums { 13 var result = 1 14 if let temp = dict[number] { 15 result = temp + 1 16 } 17 dict[number] = result 18 19 if result > limit && !resultList.contains(number){ 20 resultList.append(number) 21 } 22 } 23 24 return resultList 25 } 26 }
140ms
1 class Solution { 2 func majorityElement(_ nums: [Int]) -> [Int] { 3 var dict = [Int: Int]() 4 var r = [Int]() 5 for n in nums { 6 dict[n, default: 0] += 1 7 if dict.keys.count == 3 { 8 for key in dict.keys { 9 dict[key, default: 0] -= 1 10 if dict[key, default: 0] == 0 { 11 dict[key] = nil 12 } 13 } 14 } 15 } 16 let c = nums.reduce(into: [Int: Int]()){ $0[$01, default: 0] += 1 } 17 return Array(dict.keys).filter { c[$0, default: 0] > nums.count / 3 } 18 } 19 }
156ms
1 class Solution { 2 func majorityElement(_ nums: [Int]) -> [Int] { 3 var res = [Int:Int].init() 4 5 for i in 0..<nums.count { 6 if res.keys.contains(nums[i]){ 7 let value = res[nums[i]]!+1 8 res[nums[i]] = value 9 }else{ 10 res[nums[i]] = 1 11 } 12 } 13 14 let dict = res.filter { (arg) -> Bool in 15 16 let (_, value) = arg 17 return value > nums.count/3 18 } 19 return Array(dict.keys) 20 } 21 }
转载于:https://www.cnblogs.com/strengthen/p/10204770.html
[Swift]LeetCode229. 求众数 II | Majority Element II相关推荐
- Majority Element(169) Majority Element II(229)
寻找多数元素这一问题主要运用了:Majority Vote Alogrithm(最大投票算法) 1.Majority Element 1)description Given an array of s ...
- [LeetCode] Majority Element II
Majority Element II Given an integer array of size n, find all elements that appear more than ⌊ n/3 ...
- LeetCode 229 : Majority Element II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...
- C#LeetCode刷题之#169-求众数(Majority Element)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4048 访问. 给定一个大小为 n 的数组,找到其中的众数.众数是 ...
- leetcode 229. Majority Element II | 229. 求众数 II(找出现次数超过n/k的元素)
题目 https://leetcode.com/problems/majority-element-ii/ 题解 思路来源于左程云<程序员代码面试指南> 问题描述 原问题:给定一个整型数组 ...
- LeetCode Majority Element II(Moore Voting Algorithm即Majority Voting Algorithm)
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algo ...
- Majority Element II
https://leetcode.com/problems/majority-element-ii/#/description 挑出所有大于n/3 的数,两个很相似的题,但是这次的major 是> ...
- leetcode 229. Majority Element II(多数投票算法)
就是简单的应用多数投票算法(Boyer–Moore majority vote algorithm),参见这道题的题解. class Solution { public:vector<int&g ...
- 47 Majority Element II
原题网址; https://www.lintcode.com/problem/majority-element-ii/ 描述 给定一个整型数组,找到主元素,它在数组中的出现次数严格大于数组元素个数的三 ...
最新文章
- .net面试题大全,绝大部分面试题(有答案)
- boost::math::relative_difference相关用法的测试程序
- jq关于对象类型的判断
- 数据结构之 顺序栈、共享栈、链栈
- (转) 淘淘商城系列——redis-desktop-manager的使用
- 【ElasticSearch】Es 源码之 NamedWriteableRegistry 源码解读
- php pdo 存储过程的返回所有结果,PHP_PDO 调用存储过程,返回参数问题
- ARM 指令集版本和ARM 版本z
- DP || HYSBZ 1207 打鼹鼠
- 文件目录权限(chmod、chown、chgrp)umask、隐藏权限(lsattr、chattr)
- python 详解re模块
- Python入门学习笔记05(内置函数)
- 《码处高效:Java开发手册》之代码风格
- 淘宝和拼多多同类别测评
- MATLAB中PS是什么意思,matlab与pscad的区别
- 两个坐标系转换的变换矩阵
- 佳博Gprinter S-4331 打印机驱动
- matlab 利用函数的递归调用计算 n,关于递归:递归匿名函数Matlab
- 购买水果最便宜的方案算法题-C++实现
- java智能停车场收费管理系统
热门文章
- c# 链接mongDB集群实战开发3
- python如何收集数据库_利用Python操作mysql数据库
- 程序设计教程用c 语言编程,程序设计教程--用C 语言编程
- html的字母u代表什么意思,html元素 u 标签的使用方法及作用
- python连接sql sever_R和python连接SQL sever 数据库操作
- treeview 失去焦点时触发_33岁进央视,40岁主持《焦点访谈》的敬一丹,什么成就了她?...
- 推荐算法(二)--算法总结
- KNN(六)--LSH算法
- 【汇编语言】【ARM扩展资料】数据寻址
- RDD创建及算子分类及应用