File tree Expand file tree Collapse file tree 1 file changed +14
-13
lines changed Expand file tree Collapse file tree 1 file changed +14
-13
lines changed Original file line number Diff line number Diff line change 1
- # 2015-04-08 Runtime: 54 ms
2
-
3
- class Solution :
4
- # @param s, a string
5
- # @return an integer
1
+ # 2016-03-28 259 tests, 64 ms
2
+ class Solution (object ):
6
3
def numDecodings (self , s ):
4
+ """
5
+ :type s: str
6
+ :rtype: int
7
+ """
7
8
if not s : return 0
8
- # dp[M] means decode ways for first M letters of s
9
- dp = [0 for i in xrange ( len ( s ) + 1 )]
10
- dp [ 0 ] = 1
11
- dp [ 1 ] = 0 if s [0 ] == '0' else 1
12
- for i in xrange ( 2 , len ( s ) + 1 ):
13
- if s [ i - 1 ] != '0' : dp [ i ] += dp [ i - 1 ]
14
- if 10 <= int ( s [ i - 2 : i ]) <= 26 : dp [i ] += dp [i - 2 ]
15
- return dp [len ( s ) ]
9
+ len_s = len ( s )
10
+ dp = [1 ] + [ 0 ] * len_s
11
+ for i in xrange ( 1 , len_s + 1 ):
12
+ if s [i - 1 ] != '0' :
13
+ dp [ i ] += dp [ i - 1 ]
14
+ if i >= 2 and 10 <= int ( s [ i - 2 : i ]) <= 26 :
15
+ dp [i ] += dp [i - 2 ]
16
+ return dp [len_s ]
You can’t perform that action at this time.
0 commit comments