Skip to content

Commit dfa0f77

Browse files
committed
solve problem
1 parent f46f265 commit dfa0f77

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

decode-ways/delight010.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
func numDecodings(_ s: String) -> Int {
3+
var array = Array(s)
4+
var dp: [Int: Int] = [array.count: 1]
5+
for i in stride(from: array.count - 1, to: -1, by: -1) {
6+
if array[i] == "0" {
7+
dp[i] = 0
8+
} else {
9+
dp[i] = dp[i + 1]
10+
}
11+
12+
if i + 1 < array.count && (array[i] == "1" || array[i] == "2" && "0123456".contains(array[i + 1])) {
13+
dp[i, default: 0] += dp[i + 2] ?? 0
14+
}
15+
}
16+
return dp[0]!
17+
}
18+
}
19+

0 commit comments

Comments
 (0)