zoukankan      html  css  js  c++  java
  • Decode Ways

    A message containing letters from A-Z is 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 could be decoded as "AB" (1 2) or "L" (12).

    The number of ways decoding "12" is 2.

    class Solution {
    public:
        int numDecodings(string s) 
        {
            int size=s.length();
            if(size==0return 0;
            
            int total[size];
            if(s[0]>='1' && s[0]<='9') total[0]=1else total[0]=0;
            if(size<2return total[size-1];
            
            total[1]=0;
            if(total[0]>0 && s[1]>='1' && s[1]<='9') total[1]=1;
            
            if(10*(s[0]-'0')+s[1]-'0'>=10 && 10*(s[0]-'0')+s[1]-'0'<=26) total[1]++;
            
            for(int i=2;i<size;i++)
            {
                total[i]=0;
                if(total[i-2]>0 && 10*(s[i-1]-'0')+s[i]-'0'>=10 && 10*(s[i-1]-'0')+s[i]-'0'<=26
                    total[i]+=total[i-2];
                if(total[i-1]>0 && s[i]>'0' && s[i]<='9') total[i]+=total[i-1];
            }
            return total[size-1];
        }
    }; 
  • 相关阅读:
    android 混淆代码 -- 报错:can't find referenced class
    adb shell 删除删除指定文件夹和文件
    php GD库
    javascript的继承实现
    Mysql
    如何使用canvas画星星
    基于形态编程设计类
    算法
    腾讯web前端一面
    如何去克服害怕
  • 原文地址:https://www.cnblogs.com/erictanghu/p/3759552.html
Copyright © 2011-2022 走看看