LeetCode | Decode ways

Source: Internet
Author: User

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
     
      

 

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.