File tree Expand file tree Collapse file tree 2 files changed +33
-0
lines changed
lcof2/剑指 Offer II 004. 只出现一次的数字 Expand file tree Collapse file tree 2 files changed +33
-0
lines changed Original file line number Diff line number Diff line change @@ -150,6 +150,25 @@ function singleNumber(nums: number[]): number {
150
150
}
151
151
```
152
152
153
+ #### Swift
154
+
155
+ ``` swift
156
+ class Solution {
157
+ func singleNumber (_ nums : [Int ]) -> Int {
158
+ var ans: Int32 = 0
159
+ for i in 0 ..< 32 {
160
+ var cnt = 0
161
+ for num in nums {
162
+ cnt += (num >> i) & 1
163
+ }
164
+ cnt %= 3
165
+ ans |= Int32 (cnt) << i
166
+ }
167
+ return Int (ans)
168
+ }
169
+ }
170
+ ```
171
+
153
172
<!-- tabs: end -->
154
173
155
174
<!-- solution: end -->
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ func singleNumber( _ nums: [ Int ] ) -> Int {
3
+ var ans : Int32 = 0
4
+ for i in 0 ..< 32 {
5
+ var cnt = 0
6
+ for num in nums {
7
+ cnt += ( num >> i) & 1
8
+ }
9
+ cnt %= 3
10
+ ans |= Int32 ( cnt) << i
11
+ }
12
+ return Int ( ans)
13
+ }
14
+ }
You can’t perform that action at this time.
0 commit comments