91. Decode ways

Source: Internet
Author: User
/** 91. Decode ways * 12.20 by mingyang * meaning resolution: give you a string of numbers and decode them into English letters. * It is similar to the stair climb problem, but many restrictions need to be added. * Defining array number, number [I] means: String s [0 .. I-1] can have number [I] decoding method. * Recall the same as the stair climb problem, number [I] = number [I-1] + number [I-2] * but the difference is that the subject has a variety of restrictions: * first: s [I-1] cannot be 0, if s [I-1] is 0, number [I] can only be equal to number [I-2] --- the end number cannot be 0 * For example, the number of 110 is the same as 1 (two less) * Second, s [I-2, the first character in the I-1] cannot be 0, and integer. parseint (S. the integer obtained by substring (I-2, I) must be between 0 and 26. * The first digit of the last two digits is not 0 */Public int numdecodings (string s) {If (S = NULL | S. length () = 0) {return 0;} If (S. charat (0) = '0') {return 0;} int [] Number = new int [S. length () + 1]; number [0] = 1; number [1] = 1; int TMP; For (INT I = 2; I <= S. length (); I ++) {// check whether the current character is '0' TMP = integer. parseint (S. substring (I-1, I); If (TMP! = 0) {number [I] = number [I-1];} // check whether the current character and the previous character are combined between 1-26 if (S. charat (I-2 )! = '0') {TMP = integer. parseint (S. substring (I-2, I); If (TMP> 0 & TMP <= 26) {number [I] + = number [I-2] ;}} return number [S. length ()];}

 

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.