★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-pcmaqczz-me.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
The set S
originally contains numbers from 1 to n
. But unfortunately, due to the data error, one of the numbers in the set got duplicated to another number in the set, which results in repetition of one number and loss of another number.git
Given an array nums
representing the data status of this set after the error. Your task is to firstly find the number occurs twice and then find the number that is missing. Return them in the form of an array.github
Example 1:數組
Input: nums = [1,2,2,4] Output: [2,3]
Note:微信
集合 S
包含從1到 n
的整數。不幸的是,由於數據錯誤,致使集合裏面某一個元素複製了成了集合裏面的另一個元素的值,致使集合丟失了一個整數而且有一個元素重複。app
給定一個數組 nums
表明了集合 S
發生錯誤後的結果。你的任務是首先尋找到重複出現的整數,再找到丟失的整數,將它們以數組的形式返回。this
示例 1:spa
輸入: nums = [1,2,2,4] 輸出: [2,3]
注意:code
1 class Solution { 2 func findErrorNums(_ nums: [Int]) -> [Int] { 3 var nums = nums 4 for i in 0..<nums.count 5 { 6 while(nums[i] != nums[nums[i] - 1]) 7 { 8 nums.swapAt(i,nums[i] - 1) 9 } 10 } 11 for i in 0..<nums.count 12 { 13 if nums[i] != i + 1 14 { 15 return [nums[i], i + 1] 16 } 17 } 18 return [] 19 } 20 }
252msorm
1 class Solution { 2 func findErrorNums(_ nums: [Int]) -> [Int] { 3 var countArr = [Int](repeating: 0,count: nums.count ) 4 5 for i in 0..<nums.count{ 6 countArr[i] = -1 7 } 8 for i in 0..<nums.count{ 9 countArr[nums[i] - 1] = countArr[nums[i] - 1] + 1 10 } 11 var absent=0 12 var duplicate=0 13 14 for i in 0..<nums.count{ 15 if(countArr[i] == -1){ 16 absent = i 17 } 18 if(countArr[i] == 1){ 19 duplicate = i 20 } 21 } 22 return [duplicate + 1, absent+1] 23 } 24 }
256ms
1 class Solution { 2 func findErrorNums(_ nums: [Int]) -> [Int] { 3 var nums = nums 4 5 var errorNums: [Int] = [] 6 7 for num in nums { 8 let n = abs(num) 9 if nums[n - 1] < 0 { 10 errorNums.append(n) 11 } else { 12 nums[n - 1] *= -1 13 } 14 } 15 16 for i in 0..<nums.count { 17 if nums[i] > 0 { 18 errorNums.append(i + 1) 19 break 20 } 21 } 22 23 return errorNums 24 } 25 }
260ms
1 class Solution { 2 func findErrorNums(_ nums: [Int]) -> [Int] { 3 var counter = Array(repeating: 0, count: nums.count) 4 5 var dup = 0 6 for n in nums { 7 if counter[n - 1] == 1 { 8 dup = n 9 break 10 } 11 counter[n - 1] = 1 12 } 13 14 let l = nums.count 15 var missing = (1 + l) * l / 2 - nums.reduce(0, +) + dup 16 return [dup, missing] 17 } 18 }
272ms
1 class Solution { 2 func findErrorNums(_ nums: [Int]) -> [Int] { 3 var array = nums 4 var dup = 0 5 var missing = 0 6 for i in 0..<nums.count { 7 let index = abs(nums[i]) - 1 8 if array[index] < 0 { 9 dup = index + 1 10 } else { 11 array[index] *= -1 12 } 13 } 14 for i in 0..<array.count { 15 if array[i] > 0 { 16 missing = i + 1 17 break 18 } 19 } 20 return [dup, missing] 21 } 22 }
280ms
1 class Solution { 2 func findErrorNums(_ nums: [Int]) -> [Int] { 3 var set: Set<Int> = [] 4 var dup = 0 5 let n = nums.count 6 var compSum = 0 7 for num in nums { 8 if dup == 0 && set.contains(num) { 9 dup = num 10 } 11 if dup == 0 { 12 set.insert(num) 13 } 14 compSum += num 15 } 16 let sum = (n * (n + 1))/2 17 let diff = sum - compSum 18 return [dup, dup + diff] 19 } 20 }
296ms
1 class Solution { 2 func findErrorNums(_ nums: [Int]) -> [Int] { 3 var set = Set<Int>() 4 5 var result = [Int]() 6 7 for n in nums { 8 if set.contains(n) { 9 result.append(n) 10 } 11 set.insert(n) 12 } 13 14 for i in 1...nums.count { 15 if !set.contains(i) { 16 result.append(i) 17 } 18 } 19 return result 20 } 21 }
324ms
1 class Solution { 2 func findErrorNums(_ nums: [Int]) -> [Int] { 3 var nums = nums 4 5 var errorNums: [Int] = [] 6 7 for i in 0..<nums.count { 8 if nums[abs(nums[i]) - 1] < 0 { 9 errorNums.append(abs(nums[i])) 10 } else { 11 nums[abs(nums[i]) - 1] *= -1 12 } 13 } 14 15 for i in 0..<nums.count { 16 if nums[i] > 0 { 17 errorNums.append(i + 1) 18 break 19 } 20 } 21 22 return errorNums 23 } 24 }
340ms
1 class Solution { 2 func findErrorNums(_ nums: [Int]) -> [Int] { 3 var set = Set(1...nums.count) 4 var dup = -1 5 for x in nums { 6 if !set.contains(x) { 7 dup = x 8 } 9 set.remove(x) 10 } 11 return [dup, set.first!] 12 } 13 }
380ms
1 class Solution { 2 func findErrorNums(_ nums: [Int]) -> [Int] { 3 var seen = Set<Int>(); 4 5 var dup : Int = 0 6 var sm = 0 7 for n in nums { 8 if seen.contains(n) { 9 dup = n 10 } 11 seen.insert(n) 12 sm += n 13 } 14 let t = nums.count*(nums.count+1)/2-sm+dup 15 return [dup, t] 16 } 17 }
420ms
1 class Solution { 2 func findErrorNums(_ nums: [Int]) -> [Int] { 3 var sum = Array(1...nums.count).reduce(0, { $0 + $1 }) 4 var set = Set<Int>() 5 var res = [Int]() 6 7 for num in nums { 8 if set.contains(num) { 9 res.append(num) 10 } else { 11 set.insert(num) 12 sum -= num 13 } 14 } 15 res.append(sum) 16 17 return res 18 } 19 }