[Swift]LeetCode287. 尋找重複數 | Find the Duplicate Number

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

Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one.git

Example 1:github

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

Example 2:數組

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

Note:微信

  1. You must not modify the array (assume the array is read only).
  2. You must use only constant, O(1) extra space.
  3. Your runtime complexity should be less than O(n2).
  4. There is only one duplicate number in the array, but it could be repeated more than once.

給定一個包含 n + 1 個整數的數組 nums,其數字都在 1 到 之間(包括 1 和 n),可知至少存在一個重複的整數。假設只有一個重複的整數,找出這個重複的數。less

示例 1:spa

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

示例 2:code

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

說明:htm

  1. 不能更改原數組(假設數組是隻讀的)。
  2. 只能使用額外的 O(1) 的空間。
  3. 時間複雜度小於 O(n2) 。
  4. 數組中只有一個重複的數字,但它可能不止重複出現一次。

32msblog

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

52ms

 1 class Solution {
 2     func findDuplicate(_ nums: [Int]) -> Int {
 3         var sorted = nums.sorted()
 4         var i = 1
 5         while i < sorted.count {
 6             if sorted[i] == sorted[i - 1] {
 7                 return sorted[i]
 8             }
 9             i += 1
10         }
11         return -1
12     }
13 }

80ms

 1 class Solution {
 2     func findDuplicate(_ nums: [Int]) -> Int {
 3         var slow = 0, fast = 0, tmp = 0
 4         while true {
 5             slow = nums[slow]
 6             fast = nums[nums[fast]]
 7             if slow == fast {
 8                 break
 9             }
10         }
11         while true {
12             slow = nums[slow]
13             tmp = nums[tmp]
14             if slow == tmp {
15                 break
16             }
17         }
18         
19         return tmp
20     }
21 }

92ms

 1 class Solution {
 2     func findDuplicate(_ nums: [Int]) -> Int {
 3         var nums = nums
 4         
 5         for index in 0..<nums.count {
 6             
 7             while nums[index] != index {
 8                 if nums[index] == nums[nums[index]] {
 9                     return nums[index]
10                 }
11                 nums.swapAt(index, nums[index])
12             }
13             
14         }
15         
16         return 0
17         
18     }
19 }

96ms

 1 class Solution {
 2     func findDuplicate(_ 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                 return nums[i]
 8             }else{
 9                 res[nums[i]] = i
10             }
11         }
12         return 0
13     }
14 }

120ms

 1 class Solution {
 2     func findDuplicate(_ nums: [Int]) -> Int {
 3         
 4         var start = 0
 5         var end = nums.count
 6         
 7         while start < end {
 8             let mid = (start + end) / 2
 9             var count = 0
10             
11             nums.forEach{ if $0 <= mid { count += 1 } }
12             
13             if count > mid {
14                 end = mid
15             }else{
16                 start = mid + 1
17             }
18         }
19 
20         return start
21     }
22 }

124ms

 1 class Solution {
 2     func findDuplicate(_ nums: [Int]) -> Int {
 3         var set = Set<Int>()
 4         
 5         for item in nums {
 6             if set.contains(item) {
 7                 return item
 8             }else{
 9                 set.update(with: item)
10             }
11         }
12         
13         return 0
14     }
15 }

132ms

 1 class Solution {
 2     func findDuplicate(_ nums: [Int]) -> Int {
 3         var set = Set<Int>()
 4         for (index, num) in nums.enumerated() {
 5             set.insert(num)
 6             if index == set.count {
 7                 return num
 8             }
 9         }
10         return 0
11     }
12 }
相關文章
相關標籤/搜索