[Swift]LeetCode169. 求衆數 | Majority Element

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-tgmoeavf-md.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.git

You may assume that the array is non-empty and the majority element always exist in the array.github

Example 1:數組

Input: [3,2,3]
Output: 3

Example 2:微信

Input: [2,2,1,1,1,2,2]
Output: 2

給定一個大小爲 的數組,找到其中的衆數。衆數是指在數組中出現次數大於 ⌊ n/2 ⌋ 的元素。app

你能夠假設數組是非空的,而且給定的數組老是存在衆數。spa

示例 1:code

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

示例 2:htm

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

 1 class Solution {
 2     func majorityElement(_ nums: [Int]) -> Int {
 3         var res:Int = 0
 4         var counts:Int = 0
 5         for n in nums
 6         {
 7             if counts == 0
 8             {
 9                 res = n
10                 counts = 1
11             }
12             else if res == n
13             {
14                 counts += 1
15             }
16             else 
17             {
18                 counts -= 1
19             }
20         }
21         return res
22     }
23 }

24msblog

 1 class Solution {
 2     func majorityElement(_ nums: [Int]) -> Int {
 3         var count = 1
 4         var pre = nums[0]
 5         var result = pre
 6         for i in 1..<nums.count {
 7             if count == 0 {
 8                 count = 1
 9                 pre = nums[i]
10                 result = nums[i]
11             } else if pre == nums[i] {
12                 count += 1
13             } else {
14                 count -= 1
15             }
16             
17         }
18         
19         return result
20          
21     }
22 }

20ms

 1 class Solution {
 2     func majorityElement(_ nums: [Int]) -> Int {
 3         guard nums.count > 0 else { return 0 }
 4         var candidate = nums[0]
 5         var times = 1
 6         var i = 1
 7         while i < nums.count {
 8             if times == 0 {
 9                 candidate = nums[i]
10                 times = 1
11                 i += 1
12                 continue
13             }
14             if nums[i] == candidate {
15                 times += 1
16             } else {
17                 times -= 1
18             }
19             i += 1
20         }
21         return candidate
22     }
23 }

28ms

 1 class Solution {
 2     func majorityElement(_ nums: [Int]) -> Int {
 3         var curNum = nums[0]
 4         var majority = 0;
 5         
 6         for num in 0..<nums.count {
 7             let val = nums[num]
 8             if curNum != val {
 9                 majority -= 1
10                 if majority < 1 {
11                     curNum = val
12                     majority = 1
13                 }
14             } else {
15                 majority += 1
16             }
17         }
18         return curNum;
19     }
20 }
相關文章
相關標籤/搜索