题目:解码方法数

难度:Medium

题目内容

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.

翻译

一个包含A-Z字母的消息被编码成数字,使用以下映射:

“A” - > 1

“B”- > 2

……

“Z”- > 26

给定一个包含数字的非空字符串,确定解码它的总方法数。

我的思路:一开始想用递归但是边界问题太多,就放弃了。

      single 从0开始(每个数组单独解码),count从1开始,如果s[i-1]s[i]二者组成的数字在(0,26 ] 范围内,那就count+;

      且遇见0的时候,一开始的单独编码就不存在了,且少了一次组合的机会,例如【110】 中【11】和【0】不能组合

        所以此时 single = 0;count - 1

我的代码

     public int numDecodings(String s) {
if (s.isEmpty() || s.charAt(0) == '0')
return 0; int count = 0;
int single = 1;
for (int i = 1; i < s.length(); i++) {
if (s.charAt(i) == '0'){
count = s.length() > 2 ? count-1 : count;
single = 0;
}
int x = Integer.parseInt(s.substring(i-1, i+1));
if ( x<= 26 && x > 0) {
count += 1;
}
}
return (count + single) > 0 ? (count + single) : 0;
}

结果:214 / 258 test cases passed.

Input:"1212"
Output:4
Expected:5

编码过程中的问题

1、最初 single 和 count 没有分开计算;

2、第9行,当只有两个的时候,此时count 不需要 -1 ,例如【10】;

3、这个思路还是有问题的,例如【1212】中少计算了【12】【12】这个组合

答案代码

 public class Solution {
public int numDecodings(String s) {
int n = s.length();
if (n == 0) return 0; int[] memo = new int[n+1];
memo[n] = 1;
memo[n-1] = s.charAt(n-1) != '0' ? 1 : 0; for (int i = n - 2; i >= 0; i--)
if (s.charAt(i) == '0') continue;
else memo[i] = (Integer.parseInt(s.substring(i,i+2))<=26) ? memo[i+1]+memo[i+2] : memo[i+1]; return memo[0];
}
}

答案思路

假设所有的数字都有效,且左右两两相邻数之间也有效(1到26),那么有如下规律

   numDecodings(s) = numDecodings(s.substring(1)) + numDecodings(s.substring(2)) --------------------------------------------------式(1)

规律显而易见,斐波那契数列。不过是反过来的(从字符串后面往前)

当然,那只是假设,也要考虑特殊情况:

【使用一个数组(大小为len+1)从后往前记录对应的数字“出现”后解码数的增量

1、当前指针所指字符为0

  此时此字符无法解码,所以式(1)中的前者就只能为0,后者也为0

  例如【023】,substring(1)——【0】|【23】,截掉的【0】不能解析,所以此组合无效

         substring(2)——【02】|【3】,截掉的【02】不能解析,所以此组合无效

  所以0数字出现后,解码数的增量为0。

2、当前字符的值是有效的(大于0),但是当前字符与右边字符组合的数字无效(大于26)

  相当于式(1)中的后者=0

  例如【3212】,substring(1)——【3】|【212】,截掉的【3】能解析,所以其值为【212】的解码数

         substring(2)——【32】|【12】,截掉的【32】不能解析,所以此组合无效\

递归实现:

     public int numDecodings(String s) {
if (s.isEmpty()) {
return 1;
} if (s.charAt(0) == '0') {
return 0;
} if (s.length() == 1)
return 1; int sub1 = numDecodings(s.substring(1));
int sub2 = 0; if (Integer.parseInt(s.substring(0,2)) <= 26) {
sub2 = numDecodings(s.substring(2));
}
return sub1 + sub2;
}

此方法会在最后几个用例(很长)运行超时。

所以答案采用了迭代的方式进行。

优化:既然是斐波纳数列,那么就能使用双值迭代的方式取代用一个数组进行记录。

     public int numDecodings(String s) {
if (s.isEmpty() || s.charAt(0) == '0') {
return 0;
}
int sub1 = 1;
int sub2 = 1;
for (int i = s.length() - 2; i > -1; i--) {
if (s.charAt(i+1) == '0') {
sub1 = 0;
}
if (Integer.parseInt(s.substring(i,i+2)) <= 26) {
sub1 = sub1 + sub2;
sub2 = sub1 - sub2;
} else {
sub2 = sub1;
}
}
return sub1;
}

第11行其实还可以使用字符进行判断以降低时间和空间复杂度:

   s.charAt(i) == '1' || s.charAt(i) == '2' && s.charAt(i+1) - '0' <= 6

LeetCode第[91]题(Java):Decode Ways(解码方法数)的更多相关文章

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

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

  2. LeetCode第[18]题(Java):4Sum 标签:Array

    题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + ...

  3. LeetCode第[1]题(Java):Two Sum 标签:Array

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  4. LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2

    题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...

  5. LeetCode第[1]题(Java):Two Sum (俩数和为目标数的下标)——EASY

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  6. [LeetCode] 91. Decode Ways 解码方法

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

  7. [LeetCode] Decode Ways 解码方法

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

  8. [LintCode] Decode Ways 解码方法

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

  9. [leetcode]91. Decode Ways解码方法

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

随机推荐

  1. shell_02

    if判断: if [$? -eq 0];then echo "xxxxxxxxxxx" else echo "xxxxxxxxxxxxx" fi case判断: ...

  2. windows 最大支持线程数

    WINDOWS操作系统中可以允许最大的线程数 默认情况下,一个线程的栈要预留1M的内存空间 而一个进程中可用的内存空间只有2G,所以理论上一个进程中最多可以开2048个线程 但是内存当然不可能完全拿来 ...

  3. Java-小技巧-004-jdk时间,jdk8时间,joda,calendar,获取当前时间前一周、前一月、前一年的时间

    1.推荐使用java8 localdate等 线程安全 支持较好 地址 2.joda 一.简述 查看SampleDateFormat源码,叙述有: * Date formats are not syn ...

  4. macOS 上安装 PECL

    一.简介 PECL(The PHP Extension Community Library)是 PHP 扩展的存储库,为 PHP 所有的扩展提供提供托管和下载服务. 通过 PEAR(PHP Exten ...

  5. Linux命令 lsof使用

    lsof(list open files)是一个列出当前系统打开文件的工具.在linux环境下,任何事物都以文件的形式存在,通过文件不仅仅可以访问常规数据,还可以访问网络连接和硬件. lsof +d ...

  6. VMware+CentOS7+jdk1.7+hadoop2.4.1

    1.工具 CentOS7:去官网下载,然后找到阿里的镜像,DVD版本就好,4个G大小https://www.centos.org/download/ vmware:去官网下载最新版本 2.要点 先装V ...

  7. SVN无法Cleanup

    错误如下: 原因:svn提交遇到恶心的问题,可能是因为上次cleanup中断后,进入死循环了 解决方法:清空svn的队列 1.下载Sqlite3.exe 2.找到你项目的.svn文件,查看是否存在wc ...

  8. Java基础知识陷阱(八)

    本文发表于本人博客. 这次我来说说关于&跟&&的区别,大家都知道&是位运算符,而&&是逻辑运算符,看下面代码: public static void m ...

  9. PHP下使用Redis消息队列发布微博(复制)

    phpRedisAdmin :github地址  图形化管理界面 git clone https://github.com/ErikDubbelboer/phpRedisAdmin.git cd ph ...

  10. 技术分享会(二):SQLSERVER索引介绍

    SQLSERVER索引介绍 一.SQLSERVER索引类型? 1.聚集索引: 2.非聚集索引: 3.包含索引: 4.列存储索引: 5.无索引(堆表): 二.如何创建索引? 索引示例: 建表 creat ...