[leetcode#91] Decode Ways

Source: Internet
Author: User

problem:

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.

Analysis:

Note the transitional function!!!at first glance, it could is solved through recursive call. But we are about the total count!!!Why we must follow that routinue. The difference here is only:count the present digit as a character, or count both digits as a character. For all chacter, we should ask us the same question. Since there is only a possible situations, why not we use dynamic programming forit. The range of digits in ThisProblem is ' 00 '-' 99 ', we could intepret those, digits separately or together. Suppose Res[i] represents the sequences count from0To I.1. ' 00 'Res[i]= 0, since ' xx ' is invalid and ' 0 'is invalid.2. ' 01 '-' 09 'Res[i]= Res[i-1], must be sperated.3. ' 10 ', ' 20 'Res[i]= Res[i-2], theNewAdded character must is combined with ' 1 ' or ' 2 ' 4. ' 11-19 ', ' 21-26 'Res[i]= Res[i-2] + res[i-1]5. ' 27-99 ' not include *0Res[i]= Res[i-1]----------------------------------------------------1. ' 00 'Res[i]= 0, since ' xx ' is invalid and ' 0 'is invalid.----------------------------------------------------6. *0 (Exclude ' 10 ', ' 20 ') Res[i]= 0think the above analysis is too ugly and complex?When a' 0 ' appears, we need to tackle so many different cases. The related program must is very ugly too!Can We find a to avoid the complex checking?Yes!!!! Why not DoThe dynamic prgramming from right to left? ' 0* ' must be invalid forBoth cases, we can directly ignore it.' 01 '. ' 0 ' is invalid, and ' 01 'is also invalid. for(inti = len-1; I >= 0; i--) {    if(S.charat (i)! = ' 0 ') {        ...} If S.charat (i) is not equal to' 0 ', 1. It must could be separately intepreted ' 1 '. ' 9 'is valid.2. It might be valid forInteprrting together. ' One ' _ ' valid ' is. (note:no ' 0 'start)if(S.charat (i)! = ' 0 ') {Dp[i]= Dp[i+1]; if(I < len-1 && Integer.parseint (s.substring (i, i+2)) <= 26) Dp[i]+ = Dp[i+2];} how easy of ThisSolution, right?Skills:how Initiate the value forFirst intepreted character or ' character '. Can Useint[] DP =New int[Len];DP [0] = 1absolutely no!!!, whatifThe last character was ' 0 '. Like ' 20 'and it would BreakThe elegant code structure. How to use a extra element?int[] DP =New int[Len+1];DP [Len]= 1;1. Last characterifThe last element is ' 0 '. Dp[len-1] still equal to 0ifThe last element was not ' 0 '. Dp[i]= Dp[i+1];2. Last characterifThe second to last element is ' 0 'Dp[len-1] still equal to 0ifThe second to last element was not a ' 0 ' and in the valid range ' 11-26 'Dp[i]+ = Dp[i+2] (1); What a great skill!!!How to give the initial value forBoth CaseThrough a additional element, whileKeep the elegant code structure at the same time.

Solution:

 Public classSolution { Public intnumdecodings (String s) {if(s = =NULL|| S.length () = = 0)            return0; intLen =s.length (); int[] DP =New int[Len+1]; Dp[len]= 1;  for(inti = len-1; I >= 0; i--) {            if(S.charat (i)! = ' 0 ') {Dp[i]= Dp[i+1]; if(I < len-1 && Integer.parseint (s.substring (i, i+2)) <= 26) Dp[i]+ = Dp[i+2]; }        }        returnDp[0]; }}

[leetcode#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.