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

[Swift]LeetCode260. 只出现一次的数字 III | Single Number III

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

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

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

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

Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once.

Example:

[1,2,1,3,2,5]
[3,5]

Note:

  1. The order of the result is not important. So in the above example, [5, 3] is also correct.
  2. Your algorithm should run in linear runtime complexity. Could you implement it using only constant space complexity?

给定一个整数数组 nums,其中恰好有两个元素只出现一次,其余所有元素均出现两次。 找出只出现一次的那两个元素。

示例 :

[1,2,1,3,2,5]
[3,5]

注意:

  1. 结果输出的顺序并不重要,对于上面的例子, [5, 3] 也是正确答案。
  2. 你的算法应该具有线性时间复杂度。你能否仅使用常数空间复杂度来实现?

76ms

复制代码
 1 class Solution {
 2     func singleNumber(_ nums: [Int]) -> [Int] {
 3 
 4         var res = [0, 0]
 5         var diff = nums.reduce(0){$0 ^ $1}
 6         diff &= -diff
 7         for num in nums {
 8             if num & diff != 0 {
 9                 res[0] ^= num
10             }
11             else {
12                 res[1] ^= num
13             }
14         }
15         return res
16     }
17 }
复制代码

76ms

复制代码
 1 class Solution {
 2     func singleNumber(_ nums: [Int]) -> [Int] {
 3         guard nums.count > 1 else {
 4             return nums
 5         }
 6         
 7         var result = nums[0]
 8         for num in nums[1...] {
 9             result ^= num
10         }
11         
12         var num1 = 0, num2 = 0, tmp = 1
13         while (result & tmp) == 0 {
14             tmp <<= 1
15         }
16         
17         for num in nums {
18             if (num & tmp) > 0 {
19                 num1 ^= num
20             }
21             else {
22                 num2 ^= num
23             }
24         }
25         
26         return [num1, num2]
27     }
28 }
复制代码

80ms

复制代码
 1 class Solution {
 2     func singleNumber(_ nums: [Int]) -> [Int] {
 3         var s: Set<Int> = Set(minimumCapacity: nums.count)
 4         for i in nums {
 5             if s.contains(i) {
 6                 s.remove(i)
 7             } else {
 8                 s.insert(i)
 9             }
10         }
11         return Array(s)
12     }
13 }
复制代码

84ms

复制代码
 1 class Solution {
 2     func singleNumber(_ nums: [Int]) -> [Int] {
 3         var array = nums.sorted()
 4         // print(array)
 5         var output:[Int] = []
 6         
 7         var i = 0
 8         while i < array.count {
 9             if i == array.count - 1 || array[i] != array[i+1] {
10                 output.append(array[i])
11             } else {
12                 i += 1
13             }
14             i += 1
15             if output.count > 1 {
16                 break
17             }
18         }
19         
20         return output
21     }
22 }
复制代码

84ms

复制代码
 1 class Solution {
 2     func singleNumber(_ nums: [Int]) -> [Int] {
 3         var result = [Int]()
 4         var temp = nums.sorted()
 5         for i in 0..<temp.count {
 6             if i == 0 {
 7                 if temp[i] != temp[i+1] {
 8                     result.append(temp[i])
 9                 }
10             } else if i == temp.count - 1{
11                 if temp[i] != temp[i-1] {
12                     result.append(temp[i])
13                 }
14             }
15             else {
16                 if temp[i] != temp[i-1] && temp[i] != temp[i+1]{
17                     result.append(temp[i])
18                 }
19             }
20         }
21         return result
22     }
23 }
复制代码

 

posted @   为敢技术  阅读(251)  评论(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°