[C ++] LeetCode: 55 Decode Ways

Source: Internet
Author: User

[C ++] LeetCode: 55 Decode Ways
Question:

A message containing letters fromA-ZIs 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 cocould be decoded"AB"(1 2) or"L"(12 ).

The number of ways decoding"12"Is 2.

Ideas:

Set the dynamic array dp [I], dp [I] to represent the number of decode ways for the string s [, 2,..., I-1.

Note,If the sequence contains 0 that cannot match, the decoding method is 0.For example, the sequence 012, and 100 (the second 0 can be 10 to 1, but the third 0 cannot match ).

Two cases are discussed: Consider the last digit and the last two digits.Dynamic Planning equation:

Initial Condition: dp [0] = 1 (based on the determination of two numbers dp [2] + = dp [0 ), dp [1] = (s [0] = '0 ')? 0: 1

Recurrence Formula: 1. consider s [0, 1, 2 ,..., i-1] at the end of a number as a letter, dp [I] [1] = s [I-1] = 0? 0: dp [I-1]

2. consider s [0, 1, 2 ,..., i-1] the last two digits are considered as a letter, dp [I] [2] = isValid (s [I-2, I-1]) = true? Dp [I-2]: 0

3. dp [I] = dp [I] [1] + dp [I] [2]

Attention:

1. the setting of the initial condition dp [0] needs to be determined according to the specific meaning of the code and must meet the actual meaning (dp [2] + = dp [0], dp [0] indicates s [0, 1] indicates a letter, so 1 is taken ).

2. If there is any unmatched 0 in the sequence, the decoding method is 0. This is easy to ignore and must be noted. If a character is 0, it cannot be decoded. You only need to consider whether the character can be a valid character.

3. Add the helper Program (isValid () for legal character judgment to make the code clearer. However, you can also make a direct judgment. The Code is as follows:

for(int i = 2; i <= len; i++)        {            if(s[i-1] != '0')                dp[i] = dp[i-1];            else dp[i] = 0;            if(s[i-2] == '1' || (s[i-2] == '2' && s[i-1] <= '6'))                dp[i] += dp[i-2];        }

4. The recurrence method of dynamic equations and the definition of dp [I] must be accurate and comprehensive.

Complexity: O (N)

AC Code:

Class Solution {public: int numDecodings (string s) {int len = s. length (); if (len = 0) return 0; // dp [I] indicates s [0, 1 ,..., number of decoding methods for I-1] int dp [len + 1]; // initialize dp [0], dp [1] dp [0] = 1; if (s [0]! = '0') {dp [1] = 1;} else {dp [1] = 0;} for (int I = 2; I <= len; I ++) {// if the end number is invalid 0, dp is 0 if (isValid (s. substr (I-1, 1) {dp [I] = dp [I-1];} else {dp [I] = 0;} if (isValid (s. substr (I-2, 2) {dp [I] + = dp [I-2];} return dp [len];} public: bool isValid (string str) {// if str contains invalid 0, such as "02", decoding fails if (str [0] = '0') return false; int num = std :: stoi (str); if (num >=1 & num <= 26) {return true;} else {return false ;}}};



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.