动态规划之91 decode ways
题目链接: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] = 0if str[n] is validdp[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的更多相关文章
- Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理)
Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理) 题目描述 一条报文包含字母A-Z,使用下面的字母-数字映射进行解码 'A' -> 1 'B' -> 2 ...
- leetcode@ [91] Decode Ways (Dynamic Programming)
https://leetcode.com/problems/decode-ways/ A message containing letters from A-Z is being encoded to ...
- 【LeetCode】91. Decode Ways 解题报告(Python)
[LeetCode]91. Decode Ways 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...
- [LeetCode] 91. Decode Ways 解码方法
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- 91. Decode Ways
题目: A message containing letters from A-Z is being encoded to numbers using the following mapping: ' ...
- 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 ...
- 91. Decode Ways反编译字符串
[抄题]: A message containing letters from A-Z is being encoded to numbers using the following mapping: ...
- 91. Decode Ways(动态规划 26个字母解码个数)
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- leetcode 91 Decode Ways ----- java
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
随机推荐
- Anaconda 使用指南
Anaconda 使用指南 参考文章: 致Python初学者:Anaconda入门使用指南 Anaconda使用总结 概述 很多学习python的初学者甚至学了有一段时间的人接触到anaconda或者 ...
- Java包装
public class Test2 { public static void main(String[] args) { /*String str = "..............&qu ...
- Django后台管理系统讲解及使用
大家在创建Django项目后,在根路由urls.py文件中,会看到一行代码 from django.contrib import admin urlpatterns = [ url(r'^admin/ ...
- 2. Python3输入与输出
数据的输入和输出操作是计算机最基本的操作,本节只研究基本的输入与输出,基本输入是指从键盘上输入数据的操作,基本输出是指屏幕上显示输出结果的操作. 2.1基本输入和输出 常用的输入与输出设备有很多,如摄 ...
- 用Hexo在GitHub上搭建个人博客
我用Hexo在GitHub上搭建好了自己的博客,我的这第一篇博客就来说说搭建的过程. 1 环境配置 本文使用环境如下: Windows 10 node.js v8.1.3 git v2.13.2 np ...
- html5-article元素
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8&qu ...
- 20155228 2016-2017-2 《Java程序设计》第8周学习总结
20155228 2016-2017-2 <Java程序设计>第8周学习总结 教材学习内容总结 NIO与NIO2 NIO使用频道来衔接数据节点,在处理数据时,NIO可以让你设定缓冲区容量, ...
- CS131&Cousera图像处理学习笔记 - L5边缘
cs131: http://vision.stanford.edu/teaching/cs131_fall1617/ coursera: https://www.coursera.org/learn/ ...
- Linux环境变量和本地变量
每一种编程语言中,我们都会碰到变量的作用域的问题.(比如在函数中定义的变量在函数外不能使用的) BASH 中也有类似的问题,局部变量和环境变量(全局变量). 局部变量是普通的变量,仅在创建它的Shel ...
- STM32 定时器级联
根据参考手册给出的主/ 从定时器的例子 其实就是主定时器产生一个触发信号,让从定时器去接收这个触发信号,通过这个触发信号来让从定时器工作. 下面我们来看看我设置的从定时器 只需要配置 TIMx-> ...