一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

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.

(二)解题

题目大意:1~26代表A~Z,给定一串数字,求出能解码出多少种不同的结果。

解题思路:采用动态规划,从后往前,状态转移方程是:dp[n] = dp[n+1]+dp[n+2].

例如:123,从后往前算,3一种,23两种,123就是1+2三种,

当然要考虑很多特殊情况,如230,227,012等等

class Solution {
public:
    int numDecodings(string s) {
        int len = s.length();
        if(len==0) return 0;
        vector<int> df(len,-1);
        int num = dfsNumDecWays(s,0,df);
        return num;
    }
    int dfsNumDecWays(string &s , int idx , vector<int>& df)
    {
        int num1= 0;
        int num2=0;
        if(idx>=s.length()){//越界了就返回1
           return 1;
        }
        if(df[idx]!=-1) //代表没有访问到
        {
            return df[idx];
        }
        if(s[idx]>='1'&&s[idx]<='9') {
            num1 = dfsNumDecWays(s,idx+1,df);//求dp[n+1]
            if(idx+1<s.length())
            {
                int temp = (s[idx]-'0')*10+s[idx+1] -'0';
                if(temp>=1&&temp<=26)
                {
                    num2=dfsNumDecWays(s,idx+2,df);//求dp[n+2]
                }
            }
        }
        df[idx] = num1+num2;//状态转移方程
        return num1+num2;
    }
};

【一天一道LeetCode】#91. Decode Ways的更多相关文章

  1. leetcode@ [91] Decode Ways (Dynamic Programming)

    https://leetcode.com/problems/decode-ways/ A message containing letters from A-Z is being encoded to ...

  2. Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理)

    Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理) 题目描述 一条报文包含字母A-Z,使用下面的字母-数字映射进行解码 'A' -> 1 'B' -> 2 ...

  3. leetcode 91 Decode Ways I

    令dp[i]为从0到i的总方法数,那么很容易得出dp[i]=dp[i-1]+dp[i-2], 即当我们以i为结尾的时候,可以将i单独作为一个字母decode (dp[i-1]),同时也可以将i和i-1 ...

  4. [LeetCode] 91. Decode Ways 解码方法

    A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...

  5. leetcode 91 Decode Ways ----- java

    A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...

  6. [leetcode]91. Decode Ways解码方法

    A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...

  7. Leetcode#91 Decode Ways

    原题地址 动态规划题,注意0导致的小陷阱. 代码: int numDecodings(string s) { ] < ] > ; ] >= ] <= : ; ; int nex ...

  8. [LeetCode] 639. Decode Ways II 解码方法 II

    A message containing letters from A-Z is being encoded to numbers using the following mapping way: ' ...

  9. 【LeetCode】91. Decode Ways 解题报告(Python)

    [LeetCode]91. Decode Ways 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...

随机推荐

  1. Android编译安装失败解决办法

    今天用AndroidStudio开发了一个手机App玩玩,但是偶然遇到一个问题,自己手机上测试得劲的很,分享给朋友做测试,但是nie,意外出现了.... 两个人都给我说个安装失败,这个就比较尴尬了,找 ...

  2. Mysql bug: The server time zone value '�й���׼ʱ��' is unrecognized or represents more than one time zone.

    在 MySQL 中执行命令试下: set global time_zone='+8:00': 解释:在访问数据库时出现时区无法识别问题,在通过在数据库连接URL后,加上?serverTimezone= ...

  3. IntelliJ IDEA 14.0.3 实战搭建Spring+SpringMVC+MyBatis组合框架

    简介 Spring+SpringMVC+MyBatis框架(SSM)是比较热门的中小型企业级项目开发的框架,对于新手来说也是比较容易学习入门的.虽说容易,但在框架搭建过程中仍然遇到了许多问题,因此用实 ...

  4. MySQL my.cnf 配置文件注释

    以下是my.cnf配置文件参数解释 [client] port                     = 3309socket                   = /home/longxiben ...

  5. 深入理解null的原理

    --null的原理 --oracle一直将null和空字符串''<长度为0>同等对待<如'' is null是true,''=null为false,如果声明a varchar2:=' ...

  6. PHP echo和print 语句

    PHP  echo 和 print 语句 在 PHP 中有两个基本的输出方式: echo 和 print. 本章节中我们会详细讨论两个语句的用法,并在实例中演示如何使用 echo 和 print. P ...

  7. PHP Filter 函数

    PHP Filter 简介 PHP 过滤器用于对来自非安全来源的数据(比如用户输入)进行验证和过滤. 安装 Filter 函数是 PHP 核心的组成部分.无需安装即可使用这些函数. PHP Filte ...

  8. PHP 实例 - AJAX 实时搜索

    AJAX Live Search 在下面的实例中,我们将演示一个实时的搜索,在您键入数据的同时即可得到搜索结果. 实时的搜索与传统的搜索相比,具有很多优势: 当键入数据时,就会显示出匹配的结果 当继续 ...

  9. Docker 编辑网络配置文件

    Docker 1.2.0 开始支持在运行中的容器里编辑 /etc/hosts, /etc/hostname 和 /etc/resolve.conf 文件. 但是这些修改是临时的,只在运行的容器中保留, ...

  10. C++用LuaIntf调用Lua代码示例

    C++用LuaIntf调用Lua代码示例 (金庆的专栏 2016.12) void LuaTest::OnResponse(uint32_t uLuaRpcId, const std::string& ...