Decode Ways
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.
Problem Solving Ideas:Dynamic Planning. This topic is discussed in two ways. D[i]+=d[i-2] If the last character is jointly encoded (and eligible) with the previous character. If the last character is encoded separately (and is eligible), then d[i] + = d[i-1]. Find their own dynamic planning is not familiar with the need to improve the analysis of the problem.
Class Solution {Public:int numdecodings (string s) {int len = s.length (); if (len = = 0) {return 0; } int* D = new Int[len + 1]; Indicates the first I character has d[i-1] in decoding mode d[0] = 1; The 0-character case is known if (check (S[0])) {d[1] = 1 by analyzing 1, 2 characters; }else{d[1] = 0; } for (int i = 1; i < Len; i++) {d[i + 1] = 0; if (check (S[i-1], s[i])) {d[i+1] + = d[i-1]; } if (check (S[i])) {d[i+1] + = D[i]; }} int result = D[len]; Delete[] D; return result; }private:bool Check (char c) {if (c = = ' 0 ') {return false; }else{return true; }} bool Check (char C1, char C2) {if (c1 = = ' 0 ' | | C1 > ' 2 ' | | (c1== ' 2 ' && c2 > ' 6 ')) {return false; }else{return true; } }};
[Leetcode] Decode Ways