Skip to content

Commit 35a75a4

Browse files
authored
Update decodeWays.py
1 parent 8fab543 commit 35a75a4

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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy