LeetCode | 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:
This is also a typical DP problem. First, define an array. dp [I] is the number of all encoding methods that can be composed of the I character. For dp [I + 1], it must be at least as many as dp [I]. If the I and I + 1 characters can be merged into one character, dp [I + 1] + = dp [I-1]. However, here we need to note the invalid characters.
#include
#include
#include
using namespace std; int Decode_num(string& str){vector
vec(str.size(),0);if(str.size() <2)return 1;vec[0] =1;if(str[0]=='1' || str[1]<='6')vec[1] =2;int i;int tmp;for(i=2;i