题目描述

  A message containing letters fromA-Zis 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.

解题思路:

  解码是有规律的,所以我们可以尝试动态规划。假设数组dp[i]表示从头到字符串的第i位,一共有多少种解码方法的话,那么如果字符串的第i-1位和第i位能组成一个10到26的数字,说明我们是在第i-2位的解码方法上继续解码。如果字符串的第i-1位和第i位不能组成有效二位数字,而且第i位不是0的话,说明我们是在第i-1位的解码方法上继续解码。所以,如果两个条件都符合,则dp[i]=dp[i-1]+dp[i-2],否则dp[i]=dp[i-1]

复杂度

  时间 O(N) 空间 O(N)

注意

  如果出现无法被两位数接纳的0,则无法解码,我们可以在一开始就判断,并将其初始化为0,这样后面的相加永远都是加0

代码实现

public class Solution {
public int numDecodings(String s) {
if(s.length() == 0) return s.length();
int[] dp = new int[s.length() + 1];
// 初始化第一种解码方式
dp[0] = 1;
// 如果第一位是0,则无法解码
dp[1] = s.charAt(0) == '0' ? 0 : 1;
for(int i = 2; i <= s.length(); i++){
// 如果字符串的第i-1位和第i位能组成一个10到26的数字,说明我们可以在第i-2位的解码方法上继续解码
if(Integer.parseInt(s.substring(i-2, i)) <= 26 && Integer.parseInt(s.substring(i-2, i)) >= 10){
dp[i] += dp[i - 2];
}
// 如果字符串的第i-1位和第i位不能组成有效二位数字,在第i-1位的解码方法上继续解码
if(Integer.parseInt(s.substring(i-1, i)) != 0){
dp[i] += dp[i - 1];
}
}
return dp[s.length()];
}
}

decode-ways(动态规划)的更多相关文章

  1. decode ways(动态规划)

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

  2. Decode Ways——动态规划

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

  3. 91. Decode Ways(动态规划 26个字母解码个数)

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

  4. 动态规划小结 - 一维动态规划 - 时间复杂度 O(n),题 [LeetCode] Jump Game,Decode Ways

    引言 一维动态规划根据转移方程,复杂度一般有两种情况. func(i) 只和 func(i-1)有关,时间复杂度是O(n),这种情况下空间复杂度往往可以优化为O(1) func(i) 和 func(1 ...

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

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

  6. [LeetCode] Decode Ways 解码方法

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

  7. 44. Decode Ways && Gray Code

    Decode Ways A message containing letters from A-Z is being encoded to numbers using the following ma ...

  8. leetcode面试准备:Decode Ways

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

  9. Decode Ways leetcode java

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

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

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

随机推荐

  1. HTML5 canvas绘制雪花飘落

    Canvas是HTML5新增的组件,它就像一块幕布,可以用JavaScript在上面绘制各种图表.动画等. 没有Canvas的年代,绘图只能借助Flash插件实现,页面不得不用JavaScript和F ...

  2. JAVA_SE基础——71.Random类制作随机验证码

    public class Demo5 { public static void main(String[] args) { char[] arr={'s','b','g','h','a','c'}; ...

  3. 70后.net老猿,尚能饭否?

    程序猿的大限 距离上一次主动找工作,快到5年了,到现在的东家,是差不多3年前猎头挖过来的,而当时东家刚刚被欧洲一家有百年历史的跨国企业集团收购,所以我也就有幸成了一名“外企员工”,但是集团保留原东家人 ...

  4. APP手机端加载不到资源服务器后台解决参考

    今天发现app登录时,报could not get resource,日志中打印的是redis相关的错误,于是开始一步步检查错误! 后台架构:redis+mysql+elk+tomcat+zookee ...

  5. Mego开发文档 - 基础查询

    基础查询 Mego 使用语言集成查询(LINQ)从数据库查询数据.LINQ允许您使用C#(或其他.NET语言)根据派生的上下文和实体类编写强类型查询.将LINQ查询的表示传递给数据库提供者,翻译为数据 ...

  6. 新概念英语(1-135)The latest report

    Lesson 135 The latest report 最新消息 Listen to the tape then answer this question. Is Karen Marsh going ...

  7. api-gateway实践(08)新服务网关 - 云端发布和日志查看

    一.发布应用 1.新建应用空间 1.1.新建应用空间 1.2.新建应用 1.3.上传程序包 2.创建应用引擎服务 3.发布应用 3.1.为应用容器绑定Web运行环境(应用引擎服务) 3.2.发布应用( ...

  8. ASP.NET MVC5 Forms登陆+权限控制(控制到Action)

    一.Forms认证流程 请先参考如下网址: http://www.cnblogs.com/fish-li/archive/2012/04/15/2450571.html 本文主要介绍使用自定义的身份认 ...

  9. 高级OOP特性(6)

    PHP不支持的高级OPP特性 PHP不支持通过函数重载实现多态 PHP不支持多重继承 PHP不支持根据所修改数据类型为操作符赋予新的含义 对象克隆 克隆实例 在对象前面添加clone关键字来克隆对象, ...

  10. jquery-模仿qq提示消息

    ( function() { var ua = navigator.userAgent.toLowerCase(); var is = (ua.match(/\b(chrome|opera|safar ...