[Leedcode 91] Decode Ways

Source: Internet
Author: User

A message containing letters from was A-Z being encoded to numbers using the following mapping:

' A '-1 ' B '-2 ... ' Z '-26

Given an encoded message containing digits, determine the total number of ways to decode it.

For example,
Given encoded message "12" , it could be decoded as "AB" (1 2) or "L" (12).

The number of ways decoding is "12" 2.

 Public classSolution { Public intnumdecodings (String s) {/*The topic is similar to the number of steps in the question, so the need to use DP thought to save the middle of the sub-solution, but dp[i] is equal to dp[i-1] + dp[i-2] need to determine whether the character satisfies the requirements of the use of recursion will time out, so using the dynamic planning DP        method to do.        For string s[0...i] decoding number should be and s[0...i-1], S[0...i-2] The number of decoding is related. Dp[i]: The number of decoded on behalf of S[0...i-1], so the length of the DP is s length +1,dp[0]=dp[1]=1 dp[i] = {(s[i-1]!= ' 0 ')? dp[i-1]:0} + {s[i-2...i-1]<= ' 26 ' ?                Dp[i-2]: 0}; Sweep this string from start to finish, the string composed of s[0...i-1], how many kinds of decoding combinations, then there are two cases first: if S[I-1] corresponding to a single character can decode, then Dp[i] (S[0...i-1]) including the former dp[i-1]         The number of combinations accumulated dp[i] = dp[i-1] Second: If not only s[i-1] the corresponding single character can decode, S[i-2...i-1], two characters can also be decoded, then not only the dp[i-1] accumulated number of combinations, also including dp[i -2] bit accumulation dp[i] = Dp[i-1] + dp[i-2] We build an array of n+1, for the sake of brevity, we put a 1 in front of it. */        if(s==NULL|| S.length () <1)return0; if(S.charat (0) = = ' 0 ')return0; int[]dp=New int[S.length () +1]; dp[0]=1; Dp[1]=1;  for(intI=2;i<=s.length (); i++){            if(S.charat (i-1)! = ' 0 ') dp[i]=dp[i-1]; if(IsValid (S.substring (i-2,i))) dp[i]+=dp[i-2]; }        returndp[s.length ()]; }     Public BooleanIsValid (String s) {if(S.charat (0) = = ' 0 ')return false; if(S.charat (0) = = ' 1 ' | | (S.charat (0) = = ' 2 ' &&s.charat (1) <= ' 6 '))return true; return false; }}

[Leedcode 91] Decode Ways

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.