敢教日月换新天。为有牺牲多壮志,

[Swift]LeetCode645. 错误的集合 | Set Mismatch

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10479962.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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.

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.

Example 1:

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

Note:

  1. The given array size will in the range [2, 10000].
  2. The given array's numbers won't have any order.

集合 S 包含从1到 n 的整数。不幸的是,因为数据错误,导致集合里面某一个元素复制了成了集合里面的另外一个元素的值,导致集合丢失了一个整数并且有一个元素重复。

给定一个数组 nums 代表了集合 S 发生错误后的结果。你的任务是首先寻找到重复出现的整数,再找到丢失的整数,将它们以数组的形式返回。

示例 1:

输入: nums = [1,2,2,4]
输出: [2,3]

注意:

  1. 给定数组的长度范围是 [2, 10000]。
  2. 给定的数组是无序的。

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 }
复制代码

 

posted @   为敢技术  阅读(432)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示
哥伦布
09:09发布
哥伦布
09:09发布
3°
多云
东南风
3级
空气质量
相对湿度
47%
今天
中雨
3°/15°
周三
中雨
3°/13°
周四
小雪
-1°/6°