zoukankan      html  css  js  c++  java
  • Decode Ways

    class Solution {
    public:
        int numDecodings(string s) {
            // Start typing your C/C++ solution below
            // DO NOT write int main() function
            if(s==""||s[0]=='0')
            return 0;
            int count = s.length();
            int pre = 1;
            int cur = 1;
            int code;
            for(int i = 1;i<count;i++)
            {
                code = s[i] + (s[i-1]-'0')*10 - '0';
                if(code>26)
                {
                    if(s[i]=='0')
                    {
                        return 0;
                    }
                    else{
                        cur = cur;
                        pre = cur;
                    }
                }
                else if(code == 0)
                {
                    return 0;
                }
                else
                {
                    if(s[i]=='0')
                    {
                        cur = pre;
                        pre = 0;
                    }
                    else
                    {
                        code = cur;
                        cur = cur + pre;
                        pre = code;
                    }
                }
            }
            return cur;
        }
    };

    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.

  • 相关阅读:
    MySQL
    面向对象总结
    git指令
    DOS命令
    补充
    如何处理数据
    操作php数据库
    git安装方法
    git知识点/下一章是git的安装方法
    Css3属性
  • 原文地址:https://www.cnblogs.com/727713-chuan/p/3332953.html
Copyright © 2011-2022 走看看