We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent aed9746 commit 03066a1Copy full SHA for 03066a1
1 file changed
Kangli/DP/decodeWays
@@ -0,0 +1,23 @@
1
+
2
3
4
5
6
7
8
+O(N) time, O(1) space DP via "general fibonacci numbers"
9
+class Solution(object):
10
+ def numDecodings(self, s):
11
+ if len(s) == 0 or s[0] == '0':
12
+ return 0
13
+ prev, prev_prev = 1, 0
14
+ for i in xrange(len(s)):
15
+ cur = 0
16
+ if s[i] != '0':
17
+ cur = prev
18
+ if i > 0 and (s[i - 1] == '1' or (s[i - 1] == '2' and s[i] <= '6')):
19
+ cur += prev_prev
20
+ prev, prev_prev = cur, prev
21
+ return prev
22
23
0 commit comments