题目链接:https://leetcode-cn.com/problems/decode-ways/description/

参考:https://www.jianshu.com/p/5a604070cd11

题目大意:将一串数字,编码成A-Z的字符串。因为12-->L,或者12-->AB。所有12转成字符串一共有两种方式。该题目是求一共有多少种方案,所以可以使用dp方法。如果是求具体方案,则需要使用dfs。

状态转移方程:

(1) dp[0] = 0

(2) dp[1] = 1, if str[1] is valid
dp[1] = 0, if str[1] is not valid (3)
dp[n] = 0 if str[n] is valid
    dp[n] += dp[n - 1]
if str[n - 1] combine str[n] is valid
dp[n] += (i - 2 == 0) ? 1 : dp[n - 2]
dp[i]:前面i个字符串可以转义成多少种密文。
    public int numDecodings(String s) {
if (s.length() == 0 || s == null || s == "0")
return 0; int[] dp = new int[s.length() + 1];
dp[0] = 1; if (isValid(s.substring(0, 1)))
dp[1] = 1;
else
dp[1] = 0; for (int i = 2; i <= s.length(); i++) {
if (isValid(s.substring(i - 1, i)))
dp[i]= dp[i - 1];
if (isValid(s.substring(i - 2, i)))
dp[i]= dp[i - 2]+dp[i];
}
return dp[s.length()];
} public boolean isValid(String s) {
if (s.charAt(0) == '0')
return false;
int code = Integer.parseInt(s);
return code >= 1 && code <= 26;
}

扩展一下:如果不需要时间限制,并且需要求出具体方案。则需要使用dfs算法求出具体方案。

注意:Java中的substring(beginIndex,endIndex):大概意思返回 [beginIndex,endIndex)区间的值。所以endIndex的最大值可以是字符串的程度。

Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex.

Examples:

 "hamburger".substring(4, 8) returns "urge"
"smiles".substring(1, 5) returns "mile"
System.out.println("226".substring(3, 3));//返回为""(空)
接下来使用dfs计算出所有的可能的方式。
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map; public class Solution {
static LinkedList<String> list = new LinkedList<String>();
static LinkedList<LinkedList<String>> ans = new LinkedList<LinkedList<String>>();
Map<String, String> map = new HashMap<String, String>(); public int numDecodings(String s) {
ans.clear();
char[] alphatableb = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U',
'V', 'W', 'X', 'Y', 'Z' };
for (int i = 0; i < alphatableb.length; i++) {
map.put(i + 1 + "", alphatableb[i] + "");
}
dfs(s);
return ans.size();
} private void dfs(String s) {
if (s.isEmpty() || s.length() == 0) {
LinkedList<String> tmp = new LinkedList<String>();
tmp.addAll(list);
ans.add(tmp);
return;
}
for (int i = 1; i <= 2&&i<=s.length(); i++) {
String str = s.substring(0, i);
if (isValid(str)) {
String ss = map.get(str);
list.add(ss);
dfs(s.substring(i, s.length()));
list.pollLast();
} else {
return;
}
}
} public boolean isValid(String s) {
if (s.charAt(0) == '0')
return false;
int code = Integer.parseInt(s);
return code >= 1 && code <= 26;
} public static void main(String[] args) {
System.out.println("226".substring(3, 3));
int len = new Solution().numDecodings("226");
System.out.println(len);
System.out.println(ans);
} }
												

动态规划之91 decode ways的更多相关文章

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

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

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

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

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

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

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

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

  5. 91. Decode Ways

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

  6. 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 ...

  7. 91. Decode Ways反编译字符串

    [抄题]: A message containing letters from A-Z is being encoded to numbers using the following mapping: ...

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

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

  9. leetcode 91 Decode Ways ----- java

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

随机推荐

  1. cocos2d-X JS 获取cocostudio中的UI组件

    1.先加载cocostudio导出的json文件,代码如下所示: var dong = ccs.load("res/Login.json"); //_login.setPositi ...

  2. 在屏幕拖拽3D物体移动

    3D物体的拖拽不同于2D的.因为3D物体有x,y,z当然.实际拖拽还是在XZ平面.只是多了几个转换 using UnityEngine; using System.Collections; publi ...

  3. 通过Referer设置来防盗链

    在Servlet中需要设置防盗链功能时可以通过以下代码: String referer = request.getHeader("Referer"); if(referer == ...

  4. 使用js调用js

    直接上源码: <div class="choose"> choose a mode<br> <hr> <button type=" ...

  5. CSS position &居中(水平,垂直)

    css position是个很重要的知识点: 知乎Header部分: 知乎Header-inner部分: position属性值: fixed:生成绝对定位的元素,相对浏览器窗口进行定位(位置可通过: ...

  6. Python数据可视化-seaborn

    详细介绍可以看seaborn官方API和example galler. 1  set_style( )  set( ) set_style( )是用来设置主题的,Seaborn有五个预设好的主题: d ...

  7. 20155228 2017-5-31 课堂测试:编写MyOD.java

    20155228 2017-5-31 课堂测试:编写MyOD.java 题目和要求 编写MyOD.java:用java MyOD XXX实现Linux下od -tx -tc XXX的功能 提交测试代码 ...

  8. 什么是ASCII

    以下内容是从百度百科学的 1)ASCII(American Standard Code for Information Interchange:美国信息交换标准代码) 2)产生原因 在计算机中,所有的 ...

  9. Spring源码阅读(一)

    Spring通过配置文件或者注解对类实例进行加载管理.稍微思考,可以猜测spring加载过程肯定先把配置转化为统一的配置对象,再把通过配置对象生产类实例.阅读源码,我们也可以发现这个逻辑. sprin ...

  10. SQL数据分析概览——Hive、Impala、Spark SQL、Drill、HAWQ 以及Presto+druid

    转自infoQ! 根据 O’Reilly 2016年数据科学薪资调查显示,SQL 是数据科学领域使用最广泛的语言.大部分项目都需要一些SQL 操作,甚至有一些只需要SQL. 本文涵盖了6个开源领导者: ...