Skip to content

Commit 7796a59

Browse files
authored
feat: add swift implementation to lcof problem: No.14.1 (doocs#2866)
1 parent d449a8a commit 7796a59

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

lcof/面试题14- I. 剪绳子/README.md

+17
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,23 @@ public class Solution {
183183
}
184184
```
185185

186+
#### Swift
187+
188+
```swift
189+
class Solution {
190+
func cuttingRope(_ n: Int) -> Int {
191+
var f = [Int](repeating: 0, count: n + 1)
192+
f[1] = 1
193+
for i in 2...n {
194+
for j in 1..<i {
195+
f[i] = max(f[i], max(f[i - j] * j, (i - j) * j))
196+
}
197+
}
198+
return f[n]
199+
}
200+
}
201+
```
202+
186203
<!-- tabs:end -->
187204

188205
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
func cuttingRope(_ n: Int) -> Int {
3+
var f = [Int](repeating: 0, count: n + 1)
4+
f[1] = 1
5+
for i in 2...n {
6+
for j in 1..<i {
7+
f[i] = max(f[i], max(f[i - j] * j, (i - j) * j))
8+
}
9+
}
10+
return f[n]
11+
}
12+
}

0 commit comments

Comments
 (0)