在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ The set Given an array Example 1: Input: nums = [1,2,2,4] Output: [2,3] Note:
集合 给定一个数组 示例 1: 输入: nums = [1,2,2,4] 输出: [2,3] 注意:
Runtime: 204 ms
Memory Usage: 19.5 MB
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 } 252ms 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 }
|
请发表评论