[Leetcode] Decode Ways

Source: Internet
Author: User

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

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.