Decode Ways Problem Solving notes

Source: Internet
Author: User

Title: A-"1, B-" 2 ..... The sequence of decoding a string sequence, because there is decoding ambiguity so for example 12-"L or Existence 12-" AB, so the title is given a number string, you can give how many combinations of results:

Personal analysis:

1. There is ambiguity in the existence of 0,1,2 in a given string, so the problem of 0,1,2 should be handled well

2. If the value combination of two numbers is less than 26, there will be a variety of solutions

3. The arrangement of combinatorial learning is not good, and then look at the answer ....

Topic Category:

String, dynamic planning

Learning about Dynamic Planning:

1. Decompose the problem into several sub-problems, similar to the one in which each bit of the string s is decomposed into a sub-problem and then the solution is computed.

2. Once the solution of a given sub-problem has been calculated, then memory storage, so that the next time you need the solution of the same sub-problem to look directly at the table, set up an array, the results of each bit into an array, if the next bit results less than 26, the period result is the first two bits of sums.

3. If the addition of two bits and less than 26 is result[i+1]+result[i+2], if less than 26 is result[i+1];

4. The idea of dynamic planning is to decompose the problem:

For example, in this topic, the string is decomposed into several characters, then analyzed one by one, and the result is finally iterated.

Code:

public int numdecodings (String s) {
int n = s.length ();
if (s==null| | N==0) return 0;
Int[] result = new Int[n+1];
Result[n] = 1;
Result[n-1] = S.charat (n-1)! = ' 0 '? 1:0;
for (int i= n-2; i>=0; i--) {
if (S.charat (i) = = ' 0 ')
Continue
Else
Result[i]= (Integer.parseint (s.substring (i,i+2)) <=26)? Result[i+1]+result[i+2]:result[i+1];
}
return result[0];
}

Decode Ways Problem Solving notes

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.