[抄题]:

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

  1. 'A' -> 1
  2. 'B' -> 2
  3. ...
  4. 'Z' -> 26

Given a non-empty string containing only digits, determine the total number of ways to decode it.

Example 1:

  1. Input: "12"
  2. Output: 2
  3. Explanation: It could be decoded as "AB" (1 2) or "L" (12).

Example 2:

  1. Input: "226"
  2. Output: 3
  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 包左不包右:

  1. s.substringi, i+2

字符串转数字:

  1. Integer.parseInt(字符串)

[算法思想:递归/分治/贪心]:贪心

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

639. Decode Ways II 有* dp

[代码风格] :

  1. class Solution {
  2. public int numDecodings(String s) {
  3. //cc
  4. if (s.length() == 0) return 0;
  5.  
  6. //ini n, n - 1
  7. int n = s.length();
  8. int[] nums = new int[n + 1];
  9. nums[n] = 1;//预处理
  10. nums[n - 1] = (s.charAt(n - 1) == '0') ? 0 : 1;//处理
  11.  
  12. //for loop
  13. for (int i = n - 2; i >= 0; i--) {
  14. if (s.charAt(i) == '0') continue;
  15. nums[i] = (Integer.parseInt(s.substring(i, i + 2)) <= 26) ? nums[i + 2] + nums[i + 1] : nums[i + 1];
  16. }
  17.  
  18. return nums[0];
  19. }
  20. }

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. win7 QT +opencv环境搭建

    1.Win7 Qt4.8.5+QtCreator2.8.0+mingw环境参考前博文先搭建 2.下载Cmake2.8.11.2版本,安装.运行 [项目]那编译器选择:MinGW4.4.另外,重新编译O ...

  2. 【备忘录】CentOS服务器mysql忘记root密码恢复

    mysql的root忘记,现无法操作数据库 停止mysql服务service  mysql stop 然后使用如下的参数启动mysql, --skip-grant-tables会跳过mysql的授权 ...

  3. win7 + python2.7 安装scipy

    问题: 直接pip install scipy将不能正确安装,缺少文件 方法: 下载  "scipy‑0.19.0‑cp27‑cp27m‑win_amd64.whl"[90多M] ...

  4. Excel if函数无法正确对比大小

    我想完成以下操作 1.提取A列数字的第7-11位的数字 2.若此数字大于1993 3.则返回20,不然返回0 于是我在B和C列上写了两个函数,分别是 MID(A1,7,4)          IF(B ...

  5. [转]MySQL 经验集

    -- my.ini -> 在 [mysqld] 节点下加入一行 skip-grant-tables 然后重启服务 -- 接下来无密码登录到 mysql 执行以下命令 use mysql show ...

  6. 第二章 伪分布式安装hadoop hbase

    安装单机模式的hadoop无须配置,在这种方式下,hadoop被认为是一个单独的java进程,这种方式经常用来调试.所以我们讲下伪分布式安装hadoop. 我们继续上一章继续讲解,安装完先试试SSH装 ...

  7. Linux学习笔记 - Shell 输出命令

    1. echo 命令 echo 是基本的shell输出命令,她的语法是: echo string 我们也可以使用她来定制一些输出的格式,具体如下: 输出普通字符串 echo "it is a ...

  8. POJ-3273 Monthly Expense (最大值最小化问题)

    /* Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10757 Accepted: 4390 D ...

  9. 安装完Apache后,配置httpd.conf来使apache来加载php模块

    以apache模块的方式来安装php,在httpd.conf文件中首先使用LoadModule php5_module '.../php5apache2.dll'来动态装载Php模块,然后再用语句Ad ...

  10. [Z]shell变量详解

    原文:http://www.cnblogs.com/barrychiao/archive/2012/10/22/2733210.html 1 shell变量基础shell变量是一种很“弱”的变量,默认 ...