[Leetcode] 91. Decode Ways Java

Source: Internet
Author: User

Topic:

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.

test instructions and Analysis: the key to give a A-Z character corresponding to the number, give a string composed of a number, the need to find a possible A-Z encoding. This problem is also the topic of dynamic planning, for the character of the string I (I>2), because the maximum number of encodings is 26, so to that point may be encoded and the previous character is also related, if the current character and the previous character composed of the number D=s.charat (i-1) *10+ S.charat (i),

(1) When 0<d<=26, if d%10!=0, there are two ways to reach the I-character (from the i-1 word multibyte a character reached, from the i-2 word multibyte a character reached), i.e. F (S.charat (i)) =f (S.charat (i-1)) +f ( S.charat (i-2)); If i%10=0, there is only one way to achieve F (s.charat (i)) =f (S.charat (i-2))

(2) When d>26, if d%10!=0, then a method reaches I point (i.e. from i-1 to i), F (S.charat (i)) =f (S.charat (i-1)), if d%10=0, there is no corresponding encoding to express d these two characters, so direct return 0

(3) When the d==0, the string appears two consecutive 0, the direct return 0

The first two numeric characters of a string are processed separately, the same as above, and then traversing the string to get the final result.

Code:

public class Solution {public int numdecodings (String s) {int[] a=new int[s.length ()];//Record the Decode method for the position I of the current string        if (S.length () <=1) {if (s.length () ==0) return 0;        else{if (s.charat (0) = = ' 0 ') return 0;        else return 1;        }} if (S.length () >=2) {if (S.charat (0) = = ' 0 ')//returns 0 return 0 if 0 is included; } a[0]=1;//the first digit of the string must have only one matching method int x= (S.charat (0)-' 0 ') *10+ (S.charat (1)-' 0 ');//first two-digit value if         (x>26&&x%10==0) return 0;        if (x<=26&&x>0) if (x!=10&&x!=20) a[1]=2;        else {a[1]=1;}        else if (S.charat (1)! = ' 0 ') a[1]=1;                else {a[1]=0;} for (int i=2;i<s.length (); i++) {//record the current character and the number of previous characters, if this number is less than or equal to 26, then to the current method D (S.charat (i)) =d (S.charat (i-1)) +d (S.char        at (i-2)), otherwise D (S.charat (i)) =0 int temp= (S.charat (i-1)-' 0 ') *10+ (S.charat (i)-' 0 '); if (temp<=26&&AMP;TEMP&GT;0)//between 0 and 26 and the previous number is not 0 if (S.charat (i-1)! = ' 0 ' &&s.charat (i)! = ' 0 ') a[i]=a[i-2]+a[i-1];        else a[i]=a[i-2];        else if (temp>26)//In the middle there is greater than 30 if (temp%10!=0) a[i]=a[i-1];        else {return 0;}        The else//contains two consecutive 0 return 0;    } return A[s.length ()-1]; }}

  

[Leetcode] 91. Decode Ways Java

Related Article

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.