题目描述

  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. Centos7 Yum方式安装Mysql7

    不废话,直奔主题,可以覆盖安装. 下载并安装MySQL官方的 Yum Repository [root@localhost ~]# wget -i -c http://dev.mysql.com/ge ...

  2. linux下面根据不同的日期创建不同文件,一般用户数据库的备份的shell编程

    [root@www scripts]# vi sh03.sh #!/bin/bash # Program: #  Program creates three files, which named by ...

  3. JaveScript内置对象(JS知识点归纳八)

    1)JS自身提供的方式 用于对数据进行简便的操作,根据方法可以操作的数据类型不同,形成了不同的对象--内置对象 2)数组 ​ a)基本操作方法--对数组进行修改 从数组最后进行操作 1)数组.push ...

  4. 看漫画学Flux

    原文地址:A cartoon guide to Flux - by Lin Clark Flux在目前web开发中最受欢迎也较不被人理解,本文会以简单易懂的方式解释它. 出现问题 首先,我要声明Flu ...

  5. OpenShift实战(二):OpenShift节点扩容

    1.新增节点信息 增加节点如下,请将xxx改为自己的域名 node6.xxx.net Node 192.168.8.90 8G 20G/60G 4C node7.xxx.net Node 192.16 ...

  6. 使用pie.htc时Border-radius的兼容

    如果一个图层中(navin)使用了pie.htc来对ie6,7,8进行兼容,如若上一层(navwrap)的样式中有背景的属性,则此层 (navin) 在ie6,7,8中背景颜色不显示.如下图:此部分的 ...

  7. proxymysql的安装与应用

    具体的资料我们可以查看官方的文档:https://github.com/sysown/proxysql/wiki/ProxySQL-Configuration 推荐下载最新的Proxysql. 下面跟 ...

  8. Python-模块使用-Day6

    Python 之路 Day6 - 常用模块学习 本节大纲: 模块介绍time &datetime模块randomossysshutiljson & picleshelvexml处理ya ...

  9. SpringCloud的应用发布(二)vmvare+linux,Centos7.0下发布应用

    一.运行环境 1.jdk下载安装 地址:http://www.oracle.com/technetwork/java/javase/downloads/index.html 检查是否有老版本jdk 如 ...

  10. RxJava系列3(转换操作符)

    RxJava系列1(简介) RxJava系列2(基本概念及使用介绍) RxJava系列3(转换操作符) RxJava系列4(过滤操作符) RxJava系列5(组合操作符) RxJava系列6(从微观角 ...