高中眾數怎么求,[Swift]LeetCode229. 求眾數 II | Majority Element II

 2023-12-06 阅读 23 评论 0

摘要:★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★?微信公眾號:山青詠芝(shanqingyongzhi)?博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)?GitHub地址:https://github.com/s

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
?微信公眾號:山青詠芝(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]

三數之和 leetcode,給定一個大小為?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

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/3/190778.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息