mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-04 16:12:47 +08:00
17 lines
283 B
Go
17 lines
283 B
Go
package leetcode
|
|
|
|
func numDecodings(s string) int {
|
|
n := len(s)
|
|
dp := make([]int, n+1)
|
|
dp[0] = 1
|
|
for i := 1; i <= n; i++ {
|
|
if s[i-1] != '0' {
|
|
dp[i] += dp[i-1]
|
|
}
|
|
if i > 1 && s[i-2] != '0' && (s[i-2]-'0')*10+(s[i-1]-'0') <= 26 {
|
|
dp[i] += dp[i-2]
|
|
}
|
|
}
|
|
return dp[n]
|
|
}
|