Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions lcof2/剑指 Offer II 095. 最长公共子序列/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,30 @@ class Solution {
}
```

#### Swift

```swift
class Solution {
func longestCommonSubsequence(_ text1: String, _ text2: String) -> Int {
let m = text1.count, n = text2.count
let text1Array = Array(text1)
let text2Array = Array(text2)
var f = Array(repeating: Array(repeating: 0, count: n + 1), count: m + 1)

for i in 1...m {
for j in 1...n {
if text1Array[i - 1] == text2Array[j - 1] {
f[i][j] = f[i - 1][j - 1] + 1
} else {
f[i][j] = max(f[i - 1][j], f[i][j - 1])
}
}
}
return f[m][n]
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
19 changes: 19 additions & 0 deletions lcof2/剑指 Offer II 095. 最长公共子序列/Solution.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
func longestCommonSubsequence(_ text1: String, _ text2: String) -> Int {
let m = text1.count, n = text2.count
let text1Array = Array(text1)
let text2Array = Array(text2)
var f = Array(repeating: Array(repeating: 0, count: n + 1), count: m + 1)

for i in 1...m {
for j in 1...n {
if text1Array[i - 1] == text2Array[j - 1] {
f[i][j] = f[i - 1][j - 1] + 1
} else {
f[i][j] = max(f[i - 1][j], f[i][j - 1])
}
}
}
return f[m][n]
}
}