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 d141a87

Browse files
Create decodeWays.py
1 parent 03066a1 commit d141a87

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

‎Kangli/DP/decodeWays.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
(0)

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