Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 06b41f9

Browse files
feat: add swift implementation to lcof2 problem: No.097 (doocs#3484)
1 parent 29ab015 commit 06b41f9

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

‎lcof2/剑指 Offer II 097. 子序列的数目/README.md‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,36 @@ function numDistinct(s: string, t: string): number {
202202
}
203203
```
204204

205+
#### Swift
206+
207+
```swift
208+
class Solution {
209+
func numDistinct(_ s: String, _ t: String) -> Int {
210+
let m = s.count, n = t.count
211+
let sArray = Array(s)
212+
let tArray = Array(t)
213+
214+
var dp = Array(repeating: Array(repeating: 0, count: n + 1), count: m + 1)
215+
216+
217+
for i in 0...m {
218+
dp[i][0] = 1
219+
}
220+
221+
for i in 1...m {
222+
for j in 1...n {
223+
dp[i][j] = dp[i - 1][j]
224+
if sArray[i - 1] == tArray[j - 1] {
225+
dp[i][j] += dp[i - 1][j - 1]
226+
}
227+
}
228+
}
229+
230+
return dp[m][n]
231+
}
232+
}
233+
```
234+
205235
<!-- tabs:end -->
206236

207237
<!-- solution:end -->
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
func numDistinct(_ s: String, _ t: String) -> Int {
3+
let m = s.count, n = t.count
4+
let sArray = Array(s)
5+
let tArray = Array(t)
6+
7+
var dp = Array(repeating: Array(repeating: 0, count: n + 1), count: m + 1)
8+
9+
10+
for i in 0...m {
11+
dp[i][0] = 1
12+
}
13+
14+
for i in 1...m {
15+
for j in 1...n {
16+
dp[i][j] = dp[i - 1][j]
17+
if sArray[i - 1] == tArray[j - 1] {
18+
dp[i][j] += dp[i - 1][j - 1]
19+
}
20+
}
21+
}
22+
23+
return dp[m][n]
24+
}
25+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /