题目描述

  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. [译]RabbitMQ教程C#版 - 工作队列

    先决条件 本教程假定RabbitMQ已经安装,并运行在localhost标准端口(5672).如果你使用不同的主机.端口或证书,则需要调整连接设置. 从哪里获得帮助 如果您在阅读本教程时遇到困难,可以 ...

  2. mingw打dll ,lib包命令和调用

    1,下面的命令行将这个代码编译成 dll. gcc mydll.c -shared -o mydll.dll -Wl,--out-implib,mydll.lib 其中 -shared 告诉gcc d ...

  3. 初次面对c++

    第一次实验 2-4源码: #include<iostream> using namespace std; int main() { int day; cin>>day; swi ...

  4. Angular 学习笔记 ( CDK - Overlays )

    更新 : 2018-01-30 ng 的 overlap 在关闭的时候对 backdrop 做了一个 style pointer 目的是让 backdrop 不被 2 次点击, 但是呢,  css p ...

  5. C#程序编写规范

    代码书写规则 1.尽量使用接口,然后使用类实现接口,提高程序的灵活性. 2.一行不要超过80个字符. 3.尽量不要手工更改计算机生成的代码,若必须要改,一定要改为和计算机生成的代码风格一样. 4.关键 ...

  6. FatMouse's Speed ~(基础DP)打印路径的上升子序列

    FatMouse believes that the fatter a mouse is, the faster it runs. To disprove this, you want to take ...

  7. C# JavaScriptSerializer找不到引用

    遇到一个问题,还是第一次遇到,虽然比较简单,还是记录一下 一.写了一个小工具,为了方便就建了个Form窗体,结果用到了JavaScriptSerializer类,可是怎么都找不到System.Web. ...

  8. PV 动态供给 - 每天5分钟玩转 Docker 容器技术(153)

    前面的例子中,我们提前创建了 PV,然后通过 PVC 申请 PV 并在 Pod 中使用,这种方式叫做静态供给(Static Provision). 与之对应的是动态供给(Dynamical Provi ...

  9. CMDB资产采集

    Agent(方式) 1:服务器每台都需要安装Agent 达到采集速度快,简单:造成性能损耗 获取每台服务器的资产并有返回值:v=subprocess.getoutput('dir')或者ipconfi ...

  10. Hive函数:LAG,LEAD,FIRST_VALUE,LAST_VALUE

    参考自大数据田地:http://lxw1234.com/archives/2015/04/190.htm 测试数据准备: create external table test_data ( cooki ...