[抄题]:

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

[一句话思路]:

从前往后切不好控制范围 不知道要切几次,因此从后往前切

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 从前往后时是小角标构成大角标,从后往前是大角标构成小角标

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼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反编译字符串的更多相关文章

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

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

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

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

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

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

  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. leetcode 91 Decode Ways ----- java

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

  8. 【LeetCode】91. Decode Ways

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

  9. 【一天一道LeetCode】#91. Decode Ways

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 A messa ...

随机推荐

  1. 数据双向绑定页面无反应(angularjs)

    问题引入 使用 angularjs进行过一段时间的开发后,基本上都会遇到一个这样的坑:页面进行了双向数据绑定,控制层的数据也已经改变了,但是视图层的数据却没有改变. 其实造成这个问题的原因大致分为以下 ...

  2. Openfire源码使用Install4j打包

    https://www.ej-technologies.com/download/install4j/files 下载并安装install4jhttps://www.ej-technologies.c ...

  3. python 怎么和命令行交互

    http://www.cyberciti.biz/faq/python-run-external-command-and-get-output/ http://stackoverflow.com/qu ...

  4. c++ 异常处理(3)

    <C++编码规范与指导>一文,就已经规划着要加入这样一篇讨论 C++ 异常机制的文章了.没想到时隔几年以后才有机会把这个尾巴补完 :-). 还是那句开场白:“在恰当的场合使用恰当的特性” ...

  5. cowboy的cookie和session的例子

    session插件需要下载https://github.com/chvanikoff/cowboy_session 如果session需要分布式存储,可以参考https://github.com/sp ...

  6. 详细讲解删除SQL Server日志的具体方法

    一: 删除LOG 1:分离数据库 企业管理器->服务器->数据库->右键->分离数据库 2:删除LOG文件 3:附加数据库 企业管理器->服务器->数据库-> ...

  7. h264 aac 封装 flv

    Part 1flvtag组成 FLV 文件结构由 FLVheader和FLVBody组成.(注意flv文件是大端格式的)FLV头组成(以c为例子,一字节对齐):FLVBody是由若干个Tag组成的:  ...

  8. java代码-----循环变量的

    总结:输出相同的结果,很可能就是-个只是赋初始值, package com.mmm; public class Pnal { public static void main(String[] args ...

  9. 十九 yield方法

    yield()方法的作用是放弃当前的CPU资源,让 其他任务占用CPU执行时间.但放弃时间不确定, 有可能刚刚放弃,马上又获得CPU时间片.

  10. 5月3日上课笔记-properties文件,junit测试,mvc封层等

    StringBuffer 线程安全,效率低 StringBuilder 线程不安全,效率高 判断数组是null还是空数组 null 空数组 int[] array=null; int[] array2 ...