91. Decode Ways反编译字符串
[抄题]:
A message containing letters from A-Z is being encoded to numbers using the following mapping:
'A' -> 1
'B' -> 2
...
'Z' -> 26
Given a non-empty string containing only digits, determine the total number of ways to decode it.
Example 1:
Input: "12"
Output: 2
Explanation: It could be decoded as "AB" (1 2) or "L" (12).
Example 2:
Input: "226"
Output: 3
Explanation: It could be decoded as "BZ" (2 26), "VF" (22 6), or "BBF" (2 2 6).
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
最后一位是空串对应翻译方法只有种,为0时对应翻译方法为0
[思维问题]:
任何题目都试试dp
[一句话思路]:
从前往后切不好控制范围 不知道要切几次,因此从后往前切
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:

[一刷]:
- 从前往后时是小角标构成大角标,从后往前是大角标构成小角标
 
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
从前往后切不好控制范围 不知道要切几次,因此从后往前切
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
取出字符串长度2 包左不包右:
s.substring(i, i+2)
字符串转数字:
Integer.parseInt(字符串)
[算法思想:递归/分治/贪心]:贪心
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
639. Decode Ways II 有* dp
[代码风格] :
class Solution {
    public int numDecodings(String s) {
        //cc
        if (s.length() == 0) return 0;
        //ini n, n - 1
        int n = s.length();
        int[] nums = new int[n + 1];
        nums[n] = 1;//预处理
        nums[n - 1] = (s.charAt(n - 1) == '0') ? 0 : 1;//处理
        //for loop
        for (int i = n - 2; i >= 0; i--) {
            if (s.charAt(i) == '0') continue;
            nums[i] = (Integer.parseInt(s.substring(i, i + 2)) <= 26) ? nums[i + 2] + nums[i + 1] : nums[i + 1];
        }
        return nums[0];
    }
}
91. Decode Ways反编译字符串的更多相关文章
- Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理)
		
Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理) 题目描述 一条报文包含字母A-Z,使用下面的字母-数字映射进行解码 'A' -> 1 'B' -> 2 ...
 - 【LeetCode】91. Decode Ways 解题报告(Python)
		
[LeetCode]91. Decode Ways 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...
 - 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 解码方法
		
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 ...
 - leetcode 91  Decode Ways ----- java
		
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
 - 【LeetCode】91. Decode Ways
		
题目: A message containing letters from A-Z is being encoded to numbers using the following mapping: ' ...
 - 【一天一道LeetCode】#91. Decode Ways
		
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 A messa ...
 
随机推荐
- [嵌入式]I2C协议指东
			
最近闲来无聊,入了一块MPU6050,手头本来就有一块原子的STM32 MINI开发板,凑活着学习了一下IIC,特此总结. IIC,是集成电路总线[Inter-Intergrated Circuit] ...
 - 理解加密算法——创建CA机构,签发证书并开始TLS通信
			
1 不安全的TCP通信 普通的TCP通信数据是明文传输的,所以存在数据泄露和被篡改的风险,我们可以写一段测试代码试验一下,NODE.JS代码: TCP Server: const net=requir ...
 - java根据特定密钥对字符串进行加解密
			
package com.test; import java.io.IOException; import java.security.SecureRandom; import javax.crypto ...
 - Hibernate学习6—Hibernate 映射类型
			
第一节:基本类型映射 com.cy.model.Book.java: package com.cy.model; import java.sql.Blob; import java.util.Date ...
 - python查找字符串 函数find() 用法
			
sStr1 = 'abcdefg' sStr2 = 'cde' print sStr1.find(sStr2) 输出 2意思是在sStr1字符里的第2位置找到了包含cde字符的字段
 - SQL Server 2005/2008压缩数据库日志的方法
			
适用于SQL Server 2005的方法 Backup Log DNName WITH no_log GO DUMP TRANSACTION DNName WITH no_log GO USE DN ...
 - @Retention 注解的作用
			
注解@Retention可以用来修饰注解,是注解的注解,称为元注解.Retention注解有一个属性value,是RetentionPolicy类型的,Enum RetentionPolicy是一个枚 ...
 - SpringMVC使用Hibernate-validator验证出现的错误
			
缺少jar包 SpringMVC可以使用Hibernate-validator作为效验的实现,需要的jar包: hibernate-validator.jar validation-api.jar j ...
 - 浅谈PHP面向对象编程(二、基础知识)
			
和一些面向对象的语言有所不同,PHP并不是一种纯面向对象的语言,包PIP它支持面向对象的程序设计,并可以用于开发大型的商业程序.因此学好面向对象输程对PHP程序员来说也是至关重要的.本章并针对面向对象 ...
 - 函数和object
			
普通函数 在javascript中,函数是一等公民,函数在javascript是一个数据类型,而非像C#或其他描述性语言那样仅仅作为一个模块来使用. 一.函数调用形式 函数调用形式是最常见的形式,也是 ...