Skip to content

Commit e2467c2

Browse files
authored
feat: add swift implementation to lcci problem: No.08.05 (doocs#2687)
1 parent 3786d31 commit e2467c2

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

lcci/08.05.Recursive Mulitply/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,20 @@ impl Solution {
110110
}
111111
```
112112

113+
```swift
114+
class Solution {
115+
func multiply(_ A: Int, _ B: Int) -> Int {
116+
if B == 1 {
117+
return A
118+
}
119+
if (B & 1) == 1 {
120+
return (multiply(A, B >> 1) << 1) + A
121+
}
122+
return multiply(A, B >> 1) << 1
123+
}
124+
}
125+
```
126+
113127
<!-- tabs:end -->
114128

115129
<!-- end -->

lcci/08.05.Recursive Mulitply/README_EN.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,20 @@ impl Solution {
115115
}
116116
```
117117

118+
```swift
119+
class Solution {
120+
func multiply(_ A: Int, _ B: Int) -> Int {
121+
if B == 1 {
122+
return A
123+
}
124+
if (B & 1) == 1 {
125+
return (multiply(A, B >> 1) << 1) + A
126+
}
127+
return multiply(A, B >> 1) << 1
128+
}
129+
}
130+
```
131+
118132
<!-- tabs:end -->
119133

120134
<!-- end -->
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
func multiply(_ A: Int, _ B: Int) -> Int {
3+
if B == 1 {
4+
return A
5+
}
6+
if (B & 1) == 1 {
7+
return (multiply(A, B >> 1) << 1) + A
8+
}
9+
return multiply(A, B >> 1) << 1
10+
}
11+
}

0 commit comments

Comments
 (0)