Skip to content

Commit e31dd7a

Browse files
authored
feat: add swift implementation to lcci problem: No.05.04 (doocs#2668)
1 parent 0d8ce24 commit e31dd7a

File tree

3 files changed

+129
-0
lines changed

3 files changed

+129
-0
lines changed

lcci/05.04.Closed Number/README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,50 @@ function findClosedNumbers(num: number): number[] {
204204
}
205205
```
206206

207+
```swift
208+
class Solution {
209+
func findClosedNumbers(_ num: Int) -> [Int] {
210+
var ans = [-1, -1]
211+
let dirs = [0, 1, 0]
212+
213+
for p in 0..<2 {
214+
let a = dirs[p], b = dirs[p + 1]
215+
var x = num
216+
var found = false
217+
218+
for i in 1..<31 {
219+
if ((x >> i) & 1) == a && ((x >> (i - 1)) & 1) == b {
220+
x ^= (1 << i)
221+
x ^= (1 << (i - 1))
222+
223+
var j = 0, k = i - 2
224+
while j < k {
225+
while j < k && ((x >> j) & 1) == b {
226+
j += 1
227+
}
228+
while j < k && ((x >> k) & 1) == a {
229+
k -= 1
230+
}
231+
if j < k {
232+
x ^= (1 << j)
233+
x ^= (1 << k)
234+
}
235+
}
236+
ans[p] = x
237+
found = true
238+
break
239+
}
240+
}
241+
if !found {
242+
ans[p] = -1
243+
}
244+
}
245+
246+
return ans
247+
}
248+
}
249+
```
250+
207251
<!-- tabs:end -->
208252

209253
<!-- end -->

lcci/05.04.Closed Number/README_EN.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,50 @@ function findClosedNumbers(num: number): number[] {
208208
}
209209
```
210210

211+
```swift
212+
class Solution {
213+
func findClosedNumbers(_ num: Int) -> [Int] {
214+
var ans = [-1, -1]
215+
let dirs = [0, 1, 0]
216+
217+
for p in 0..<2 {
218+
let a = dirs[p], b = dirs[p + 1]
219+
var x = num
220+
var found = false
221+
222+
for i in 1..<31 {
223+
if ((x >> i) & 1) == a && ((x >> (i - 1)) & 1) == b {
224+
x ^= (1 << i)
225+
x ^= (1 << (i - 1))
226+
227+
var j = 0, k = i - 2
228+
while j < k {
229+
while j < k && ((x >> j) & 1) == b {
230+
j += 1
231+
}
232+
while j < k && ((x >> k) & 1) == a {
233+
k -= 1
234+
}
235+
if j < k {
236+
x ^= (1 << j)
237+
x ^= (1 << k)
238+
}
239+
}
240+
ans[p] = x
241+
found = true
242+
break
243+
}
244+
}
245+
if !found {
246+
ans[p] = -1
247+
}
248+
}
249+
250+
return ans
251+
}
252+
}
253+
```
254+
211255
<!-- tabs:end -->
212256

213257
<!-- end -->
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
class Solution {
2+
func findClosedNumbers(_ num: Int) -> [Int] {
3+
var ans = [-1, -1]
4+
let dirs = [0, 1, 0]
5+
6+
for p in 0..<2 {
7+
let a = dirs[p], b = dirs[p + 1]
8+
var x = num
9+
var found = false
10+
11+
for i in 1..<31 {
12+
if ((x >> i) & 1) == a && ((x >> (i - 1)) & 1) == b {
13+
x ^= (1 << i)
14+
x ^= (1 << (i - 1))
15+
16+
var j = 0, k = i - 2
17+
while j < k {
18+
while j < k && ((x >> j) & 1) == b {
19+
j += 1
20+
}
21+
while j < k && ((x >> k) & 1) == a {
22+
k -= 1
23+
}
24+
if j < k {
25+
x ^= (1 << j)
26+
x ^= (1 << k)
27+
}
28+
}
29+
ans[p] = x
30+
found = true
31+
break
32+
}
33+
}
34+
if !found {
35+
ans[p] = -1
36+
}
37+
}
38+
39+
return ans
40+
}
41+
}

0 commit comments

Comments
 (0)