File tree Expand file tree Collapse file tree 2 files changed +43
-0
lines changed
lcof/面试题56 - I. 数组中数字出现的次数 Expand file tree Collapse file tree 2 files changed +43
-0
lines changed Original file line number Diff line number Diff line change @@ -201,6 +201,30 @@ public class Solution {
201
201
}
202
202
```
203
203
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
+
204
228
<!-- tabs: end -->
205
229
206
230
<!-- solution: end -->
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments