Skip to content

Commit 31290df

Browse files
authored
feat: add swift implementation to lcof2 problem: No.067 (#3181)
1 parent e4d90d7 commit 31290df

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

lcof2/剑指 Offer II 067. 最大的异或/README.md

+28
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,34 @@ func findMaximumXOR(nums []int) int {
235235
}
236236
```
237237

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+
238266
<!-- tabs:end -->
239267

240268
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
}

0 commit comments

Comments
 (0)