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];
        }
    }; 
  • 相关阅读:
    HTTP状态码
    firefox浏览器新建页面一直处于刷新状态解决方法
    firefox浏览器需要新建窗口时以新建标签页代替
    bash基础教程
    sqoop的导入|Hive|Hbase
    sqoop导出数据|Hive|HDFS和脚本编写
    sqoop安装
    sqoop简介和原理分析
    Oozie安装
    Oozie框架介绍
  • 原文地址:https://www.cnblogs.com/erictanghu/p/3759552.html
Copyright © 2011-2022 走看看