File tree 2 files changed +51
-0
lines changed
lcof2/剑指 Offer II 067. 最大的异或
2 files changed +51
-0
lines changed Original file line number Diff line number Diff line change @@ -235,6 +235,34 @@ func findMaximumXOR(nums []int) int {
235
235
}
236
236
```
237
237
238
+ #### Swift
239
+
240
+ ``` swift
241
+ class Solution {
242
+ func findMaximumXOR (_ numbers : [Int ]) -> Int {
243
+ var max = 0
244
+ var mask = 0
245
+
246
+ for i in stride (from : 30 , through : 0 , by : -1 ) {
247
+ let current = 1 << i
248
+ mask ^= current
249
+ var set = Set < Int > ()
250
+ for num in numbers {
251
+ set .insert (mask & num)
252
+ }
253
+ let flag = max | current
254
+ for prefix in set {
255
+ if set .contains (prefix ^ flag) {
256
+ max = flag
257
+ break
258
+ }
259
+ }
260
+ }
261
+ return max
262
+ }
263
+ }
264
+ ```
265
+
238
266
<!-- tabs:end -->
239
267
240
268
<!-- solution:end -->
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ func findMaximumXOR( _ numbers: [ Int ] ) -> Int {
3
+ var max = 0
4
+ var mask = 0
5
+
6
+ for i in stride ( from: 30 , through: 0 , by: - 1 ) {
7
+ let current = 1 << i
8
+ mask ^= current
9
+ var set = Set < Int > ( )
10
+ for num in numbers {
11
+ set. insert ( mask & num)
12
+ }
13
+ let flag = max | current
14
+ for prefix in set {
15
+ if set. contains ( prefix ^ flag) {
16
+ max = flag
17
+ break
18
+ }
19
+ }
20
+ }
21
+ return max
22
+ }
23
+ }
You can’t perform that action at this time.
0 commit comments