mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-04 16:12:47 +08:00
update 91
This commit is contained in:
@ -1,29 +1,16 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func numDecodings(s string) int {
|
||||
if len(s) == 0 {
|
||||
return 0
|
||||
}
|
||||
dp := make([]int, len(s)+1)
|
||||
n := len(s)
|
||||
dp := make([]int, n+1)
|
||||
dp[0] = 1
|
||||
if s[:1] == "0" {
|
||||
dp[1] = 0
|
||||
} else {
|
||||
dp[1] = 1
|
||||
}
|
||||
for i := 2; i <= len(s); i++ {
|
||||
lastNum, _ := strconv.Atoi(s[i-1 : i])
|
||||
if lastNum >= 1 && lastNum <= 9 {
|
||||
for i := 1; i <= n; i++ {
|
||||
if s[i-1] != '0' {
|
||||
dp[i] += dp[i-1]
|
||||
}
|
||||
lastNum, _ = strconv.Atoi(s[i-2 : i])
|
||||
if lastNum >= 10 && lastNum <= 26 {
|
||||
if i > 1 && s[i-2] != '0' && (s[i-2]-'0')*10+(s[i-1]-'0') <= 26 {
|
||||
dp[i] += dp[i-2]
|
||||
}
|
||||
}
|
||||
return dp[len(s)]
|
||||
return dp[n]
|
||||
}
|
||||
|
@ -51,32 +51,19 @@ Given a **non-empty** string containing only digits, determine the total numbe
|
||||
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func numDecodings(s string) int {
|
||||
if len(s) == 0 {
|
||||
return 0
|
||||
}
|
||||
dp := make([]int, len(s)+1)
|
||||
n := len(s)
|
||||
dp := make([]int, n+1)
|
||||
dp[0] = 1
|
||||
if s[:1] == "0" {
|
||||
dp[1] = 0
|
||||
} else {
|
||||
dp[1] = 1
|
||||
}
|
||||
for i := 2; i <= len(s); i++ {
|
||||
lastNum, _ := strconv.Atoi(s[i-1 : i])
|
||||
if lastNum >= 1 && lastNum <= 9 {
|
||||
for i := 1; i <= n; i++ {
|
||||
if s[i-1] != '0' {
|
||||
dp[i] += dp[i-1]
|
||||
}
|
||||
lastNum, _ = strconv.Atoi(s[i-2 : i])
|
||||
if lastNum >= 10 && lastNum <= 26 {
|
||||
if i > 1 && s[i-2] != '0' && (s[i-2]-'0')*10+(s[i-1]-'0') <= 26 {
|
||||
dp[i] += dp[i-2]
|
||||
}
|
||||
}
|
||||
return dp[len(s)]
|
||||
return dp[n]
|
||||
}
|
||||
|
||||
```
|
||||
|
Reference in New Issue
Block a user