zoukankan      html  css  js  c++  java
  • leetcode[91]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 check(char  one)
    {
        int tmp=one-'0';
        return (tmp>=1&&tmp<=9)?1:0;
    //    return one=='0'?0:1;
    }
    int check(char one, char two)
    {
        int tmp=10*(one-'0')+two-'0';
        return (tmp>=10&&tmp<=26)?1:0;
    //    return (one=='1'||(one=='2'&&two<='6'))?1:0;
    }
    int numDecodings(string s) 
    {
        if(s.empty())return 0;
        int len=s.length();
        vector<int> f(len,0);
        if(len==1)
        {
            f[0]=check(s[0]);
            return f[0];
        }
        f[0]=check(s[0]);
        f[1]=(check(s[0])&&check(s[1]))+check(s[0],s[1]);
        for (int i=2;i<len;i++)
        {
            if(check(s[i]))f[i]+=f[i-1];
            if(check(s[i-1],s[i]))f[i]+=f[i-2];
        }
        return f[len-1];
    }
    };
  • 相关阅读:
    HTML-图片和多媒体
    HTML弹性布局
    HTML定位——绝对定位和相对定位、固定定位
    HTML定位和布局----float浮动
    CSS层叠
    HTML-css样式引用方式
    认识HTML和CSS
    原生js 进度条
    原生js文字滚动 滚动条
    时间轴
  • 原文地址:https://www.cnblogs.com/Vae1990Silence/p/4281391.html
Copyright © 2011-2022 走看看