Skip to content

Commit ce712fc

Browse files
committed
91. Decode Ways Solution
1 parent 94ee1a8 commit ce712fc

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

decode-ways/doh6077.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
def numDecodings(self, s: str) -> int:
3+
if s[0] == '0':
4+
return 0
5+
n = len(s)
6+
cur = 1
7+
pre1 = pre2 = 1
8+
for i in range(2, n+1):
9+
one = int(s[i-1])
10+
two = int(s[i-2:i])
11+
cur = 0
12+
if 1 <= one <= 9:
13+
cur += pre1
14+
if 10 <= two <= 26:
15+
cur += pre2
16+
17+
pre2 = pre1
18+
pre1 = cur
19+
20+
return cur

0 commit comments

Comments
 (0)