8000 91, dp · chaor/LeetCode_Python_Accepted@3af7680 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3af7680

Browse files
committed
91, dp
1 parent 2316872 commit 3af7680

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

91_Decode_Ways.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
# 2015-04-08 Runtime: 54 ms
2-
3-
class Solution:
4-
# @param s, a string
5-
# @return an integer
1+
# 2016-03-28 259 tests, 64 ms
2+
class Solution(object):
63
def numDecodings(self, s):
4+
"""
5+
:type s: str
6+
:rtype: int
7+
"""
78
if not s: return 0
8-
# dp[M] means decode ways for first M letters of s
9-
dp = [0 for i in xrange(len(s) + 1)]
10-
dp[0] = 1
11-
dp[1] = 0 if s[0] == '0' else 1
12-
for i in xrange(2, len(s) + 1):
13-
if s[i-1] != '0': dp[i] += dp[i-1]
14-
if 10 <= int(s[i-2:i]) <= 26: dp[i] += dp[i-2]
15-
return dp[len(s)]
9+
len_s = len(s)
10+
dp = [1] + [0] * len_s
11+
for i in xrange(1, len_s + 1):
12+
if s[i - 1] != '0':
13+
dp[i] += dp[i - 1]
14+
if i >= 2 and 10 <= int(s[i - 2: i]) <= 26:
15+
dp[i] += dp[i - 2]
16+
return dp[len_s]

0 commit comments

Comments
 (0)
0