Skip to content

Commit 6c8f08c

Browse files
authored
feat: add swift implementation to lcci problem: No.05.07 (doocs#2670)
1 parent e31dd7a commit 6c8f08c

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

lcci/05.07.Exchange/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,18 @@ impl Solution {
8181
}
8282
```
8383

84+
```swift
85+
class Solution {
86+
func exchangeBits(_ num: Int) -> Int {
87+
let oddShifted = (num & 0x55555555) << 1
88+
89+
let evenShifted = (num & 0xaaaaaaaa) >> 1
90+
91+
return oddShifted | evenShifted
92+
}
93+
}
94+
```
95+
8496
<!-- tabs:end -->
8597

8698
<!-- end -->

lcci/05.07.Exchange/README_EN.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,18 @@ impl Solution {
8787
}
8888
```
8989

90+
```swift
91+
class Solution {
92+
func exchangeBits(_ num: Int) -> Int {
93+
let oddShifted = (num & 0x55555555) << 1
94+
95+
let evenShifted = (num & 0xaaaaaaaa) >> 1
96+
97+
return oddShifted | evenShifted
98+
}
99+
}
100+
```
101+
90102
<!-- tabs:end -->
91103

92104
<!-- end -->

lcci/05.07.Exchange/Solution.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution {
2+
func exchangeBits(_ num: Int) -> Int {
3+
let oddShifted = (num & 0x55555555) << 1
4+
5+
let evenShifted = (num & 0xaaaaaaaa) >> 1
6+
7+
return oddShifted | evenShifted
8+
}
9+
}

0 commit comments

Comments
 (0)