We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 8fab543 commit 35a75a4Copy full SHA for 35a75a4
Kangli/DP/decodeWays.py
@@ -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)]
15
-
-O(N) time, O(1) space DP via "general fibonacci numbers"
16
+
17
+#O(N) time, O(1) space DP via "general fibonacci numbers"
18
class Solution(object):
19
def numDecodings(self, s):
20
if len(s) == 0 or s[0] == '0':
0 commit comments