Skip to content

Commit 8468fd1

Browse files
authored
decode ways solution
1 parent 8449524 commit 8468fd1

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

decode-ways/yhkee0404.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
func numDecodings(s string) int {
2+
dp := make([]int, len(s) + 1)
3+
dp[0] = 1
4+
const cnt = rune('Z') - rune('A') + 1
5+
for i, c := range s {
6+
a := 0
7+
if i != 0 {
8+
b := (rune(s[i - 1]) - rune('0')) * 10 + rune(c) - rune('0')
9+
if b > 9 && b <= cnt {
10+
a += dp[i - 1]
11+
}
12+
}
13+
b := rune(c) - rune('0')
14+
if b != 0 && b < cnt {
15+
a += dp[i]
16+
}
17+
dp[i + 1] = a
18+
}
19+
return dp[len(s)]
20+
}

0 commit comments

Comments
 (0)