class Solution: # @param s, a string # @return an integer def numDecodings(self, s): n=len(s) dp=[0]*(n) if n==0: return 0 if s[0]!='0': dp[0]=1 else: return 0 for i in range(1,n): if s[i]=='0': if s[i-1] in '12': if i<2: dp[i]=1 else: dp[i]=dp[i-2] else: return 0 elif s[i-1]=='0': dp[i]=dp[i-1] else: if int(s[i-1:i+1])<=26: if i<2: dp[i]=2 else: dp[i]=dp[i-1]+dp[i-2] else: dp[i]=dp[i-1] return dp[n-1]
Friday, September 19, 2014
Leetcode: Decode Ways @Python
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment