• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[Swift]LeetCode645.错误的集合|SetMismatch

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(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 }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
swift-03-构造器(Designated&amp;&amp;Convenience)发布时间:2022-07-13
下一篇:
[Swift]LeetCode27.移除元素|RemoveElement发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap