Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9437beb

Browse files
Update decodeWays.py
1 parent 45d0826 commit 9437beb

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

‎Kangli/DP/decodeWays.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
1+
#first solution, O(N) time and space
2+
class Solution(object):
3+
def numDecodings(self, s):
4+
if not s:
5+
return 0
6+
dp = [0]*(len(s)+1)
7+
dp[0] = 1
8+
for i in range(1, len(s)+1):
9+
if s[i-1] != '0':
10+
dp[i] += dp[i-1]
11+
if i != 1:
12+
if int(s[i-2:i]) in range(10, 27):
13+
dp[i] += dp[i-2]
14+
return dp[len(s)]
115

2-
3-
4-
5-
6-
7-
8-
O(N) time, O(1) space DP via "general fibonacci numbers"
16+
17+
#O(N) time, O(1) space DP via "general fibonacci numbers"
918
class Solution(object):
1019
def numDecodings(self, s):
1120
if len(s) == 0 or s[0] == '0':

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /