File tree Expand file tree Collapse file tree 2 files changed +55
-0
lines changed
lcof2/剑指 Offer II 097. 子序列的数目 Expand file tree Collapse file tree 2 files changed +55
-0
lines changed Original file line number Diff line number Diff 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 -->
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments