[C + +] Leetcode:55 Decode Ways

Source: Internet
Author: User

Title:

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 " , it could be decoded as " AB "   (1 2) Or " L "   ().

The number of ways decoding is "12" 2.

Ideas:

Set dynamic array dp[i], Dp[i] represents the number of decode means for string s[0,1,2,...., i-1].

Note that if there is a mismatch of 0 in the sequence, then the decoding method is 0, such as the sequence 012,,100 (the second 0 can and 1 composition 10, but the third 0 does not match).

Divided into two cases, consider the end of a number and consider the end of two digits. dynamic Programming equation:

Initial conditions: dp[0] = 1 (according to judging two digits dp[2] + = dp[0] ok), dp[1] = (s[0] = = ' 0 ')? 0:1

Recursive 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] At the end of two digits 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 conditions dp[0], need to be based on the specific meaning of the code to determine, to meet the actual meaning (dp[2] + = Dp[0], here dp[0] means s[0,1] for a letter, so take 1) .

2. If there is a mismatch of 0 in the sequence, then the decoding method is 0. This is easy to overlook, so be sure to pay attention. If a character is 0, it cannot be decoded, only considering whether it can make a valid character from the previous character.

3. The helper Program (IsValid ()) that adds legal character judgments can make the code more clear. But you can also take 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 recursive way of dynamic equations, and the meaning of dp[i], need to be accurate and considered comprehensively.

Complexity: O (N)

AC Code:

Class Solution {Public:int numdecodings (string s) {int len = s.length ();                if (len = = 0) return 0;                Dp[i] Indicates the number of decoding methods of s[0,1,..., 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 trailing number is invalid 0, then 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 <=) {return true; } else {return false; }    }};



[C + +] Leetcode:55 Decode Ways

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.