[抄题]:

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. Oracle之 等待事件log file sync + log file parallel write (awr优化)

    这是3月份某客户的情况,原因是server硬件故障后进行更换之后,业务翻译偶尔出现提交缓慢的情况.我们先来看下awr的情况. 我们能够看到,该系统的load profile信息事实上并不高,每秒才21 ...

  2. centos下svn的主要常用命令(解决商城系统添加的文件无法自动更新至svn服务器)

    问题描述: 在商城中通过网页上传的png文件无法自动添加到版本库中. 查找过程: 通过程序分析,增加的主要是数据文件,主要分布在data目录中. svn list /home/ggg --depth= ...

  3. java代码--------打印三角形

    总结:这里主要是for循环里的j<=i而不死j<=i-1;.还有先打印“*” 再打印空格.换行.理解.请用脑子啊 package com.sads; public class Dds { ...

  4. python 将html实体转回去

    参考资料: http://www.360doc.com/content/17/0620/16/44530822_664927373.shtml https://blog.csdn.net/guzhou ...

  5. Lock分析

      Lock接口是锁的实现,用来控制多个线程访问共享资源的方式,是在java 1.5的时候引入的,在此之前,只能通过synchronized的方式来取得对象的锁. synchronized中的锁是隐式 ...

  6. Zabbix 添加 WEB 监控

    添加 WEB Monitorings Web Monitoring是用来监控web程序的,可以监控到web程序的下载速度.返回码及响应时间,还支持把一组连续的web动作作为一个整体来监控. 下面我们以 ...

  7. hadoop集群调优-OS和文件系统部分

    OS and File System 根据Dell(因为我们的硬件采用dell的方案)关于hadoop调优的相关说明,改变几个Linux的默认设置,Hadoop的性能能够增长大概15%. open f ...

  8. 启动 Eclipse 报错 “An internal error occurred during: "Initializing Java Tooling". java.lang.NullPointerException”

    之前在线升级了Eclipse,由于网络/或者是设置问题,在升级完成后启动Eclipse出线上述错误... 解决方法 1. 删除目录工作目录下面的.project文件夹: 如下图: 2. 关闭Eclip ...

  9. Jsp+Servlet+JDBC的使用复习

    最近对JDBC进行了复习,对事物的理解,连接池的使用等部分都有一个复习,所以使用Servlet+JDBC完成了一个小Demo,在这里对这种底层的操作进行总结.框架的使用的确方便了我们的开发,但是底层的 ...

  10. An Autofac Lifetime Primer

    Or, “Avoiding Memory Leaks in Managed Composition” Understanding lifetime can be pretty tough when y ...