Skip to content

Commit 8ebdd67

Browse files
authored
feat: add swift implementation to lcof problem: No.56.1 (doocs#2944)
1 parent 3bca478 commit 8ebdd67

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

lcof/面试题56 - I. 数组中数字出现的次数/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,30 @@ public class Solution {
201201
}
202202
```
203203

204+
#### Swift
205+
206+
```swift
207+
class Solution {
208+
func singleNumbers(_ nums: [Int]) -> [Int] {
209+
var xorSum = 0
210+
for num in nums {
211+
xorSum ^= num
212+
}
213+
214+
let lowBit = xorSum & -xorSum
215+
var a = 0
216+
for num in nums {
217+
if (num & lowBit) != 0 {
218+
a ^= num
219+
}
220+
}
221+
222+
let b = xorSum ^ a
223+
return [a, b]
224+
}
225+
}
226+
```
227+
204228
<!-- tabs:end -->
205229

206230
<!-- solution:end -->
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
func singleNumbers(_ nums: [Int]) -> [Int] {
3+
var xorSum = 0
4+
for num in nums {
5+
xorSum ^= num
6+
}
7+
8+
let lowBit = xorSum & -xorSum
9+
var a = 0
10+
for num in nums {
11+
if (num & lowBit) != 0 {
12+
a ^= num
13+
}
14+
}
15+
16+
let b = xorSum ^ a
17+
return [a, b]
18+
}
19+
}

0 commit comments

Comments
 (0)