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

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

Given an encoded message containing digits, determine the total number of ways to decode it.

For example,
Given encoded message "12", it could be decoded as "AB" (1 2) or "L" (12).

The number of ways decoding "12" is 2.

按照题目中的规则,给定一个字符串,然后判断有多少种原始的组合方式。

1、基本方法是用动态规划,很容易想到,如果i>2,同时num[i-1]与num[i]这两个数字是在11-26之间,那么dp[i] = dp[i-1]+dp[i-2]  

2、容易出错的点在于对于0的判断,如果0开头,那么直接返回0,还有就是如果num[i]==0的时候,需要根据num[i-1]的值判断是该返回0,还是dp[i] = dp[i-2]。

其实这道题这样就已经做完了,但是

3、我先是看到了输入是String而不是int[],我以为输入中是可以带有字母的,而我用字母测试的时候,也是通过的。所以我的做法中带入了判断字母的部分,所以最后结果是2ms,并不是最快的1ms。

public class Solution {
public int numDecodings(String s) {
int len = s.length();
if( len == 0)
return 0;
else if( len == 1)
return s.charAt(0) == '0'?0:1;
int[] num = new int[len];
for( int i = 0;i<len;i++){
if( s.charAt(i) >= '0' && s.charAt(i) <='9')
num[i] = s.charAt(i)-'0';
else
num[i] = -1;
}
for( int i = 0;i <len ;i++)
num[i] = s.charAt(i)-'0';
int[] dp = new int[len];
if( num[0] == 0)
return 0;
else
dp[0] = 1;
if( num[1] == 0){
if( (num[0] >2 || num[0]<1) )
return 0;
else
dp[1] = 1;
}else if( (num[0] == 1 && num[1] > 0) || (num[0] == 2 && num[1]<7 && num[1] > 0))
dp[1] = 2;
else
dp[1] = dp[0];
for( int i = 2;i<len;i++){
if( num[i] == 0){
if( num[i-1] >2 || num[i-1] < 1 )
return 0;
else
dp[i] = dp[i-2];
}
else if( (num[i-1] == 1 && num[i] > 0) || (num[i-1] == 2 && num[i] < 7 ))
dp[i] = dp[i-1]+dp[i-2];
else
dp[i] = dp[i-1]; }
return dp[len-1];
}
}

下面是discuss中的某个代码,并没有判断字母。

public class Solution {
public int numDecodings(String s) {
if(s == null || s.length() == 0) {
return 0;
}
int n = s.length();
int[] dp = new int[n+1];
dp[0] = 1;
dp[1] = s.charAt(0) != '0' ? 1 : 0;
for(int i = 2; i <= n; i++) {
int first = Integer.valueOf(s.substring(i-1, i));
int second = Integer.valueOf(s.substring(i-2, i));
if(first >= 1 && first <= 9) {
dp[i] += dp[i-1];
}
if(second >= 10 && second <= 26) {
dp[i] += dp[i-2];
}
}
return dp[n];
}
}

leetcode 91 Decode Ways ----- java的更多相关文章

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

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

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

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

  3. 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 ...

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

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

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

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

  6. Leetcode#91 Decode Ways

    原题地址 动态规划题,注意0导致的小陷阱. 代码: int numDecodings(string s) { ] < ] > ; ] >= ] <= : ; ; int nex ...

  7. [LeetCode] 639. Decode Ways II 解码方法 II

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

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

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

  9. 91. Decode Ways

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

随机推荐

  1. IT公司100题-9-判断整数序列是不是二元查找树的后序遍历结果

    问题描述: 输入一个整数数组,判断该数组是不是某二元查找树的后序遍历的结果. 如果是返回true,否则返回false. 例如输入4, 8, 6, 12, 16, 14, 10,由于这一整数序列是如下树 ...

  2. 经典线程同步 互斥量Mutex

    阅读本篇之前推荐阅读以下姊妹篇: <秒杀多线程第四篇一个经典的多线程同步问题> <秒杀多线程第五篇经典线程同步关键段CS> <秒杀多线程第六篇经典线程同步事件Event& ...

  3. static关键字的理解

    #include<stdio.h> int counter(int i){ static int count=0;//编译时只运行一次 count=count+i; return coun ...

  4. SSH(1)

    假定服务器ip为192.168.1.139,ssh服务的端口号为22,服务器上有个用户为pi,两边都是ubuntu 一,Init *安装 如果是想用ssh从本机登陆别的机器,只需要安装openssh- ...

  5. 将Ajax 中数组转换成字符串 封装成类

    <?php class Ajax{ //ajax调用的方法 //sql是要执行的语句 //$type是SQL语句的类型,0代表增删改,1代表查询 //$db代表要操作的数据 public fun ...

  6. 一篇文章教你学会基础的HTML

    html是学习做网页的基础,漂亮的网页与布局就是由有些html代码组成,大家看完这篇文章就可以简单的了解html了,多写多练     如果你不致力于成为美工的话,那么作为开发人员,可以读懂HTML.必 ...

  7. ListView Optimization

    ListView仅仅缓存了可视范围内的View,随后的滚动都是对这些View进行数据更新 1.static inner class ViewHolder and setTag() 2.use conv ...

  8. hdu 2097

    ps:WA了两次好像....Sky数是三个进制下的各位数之和相等...而不是都等于22...我傻逼了... 代码: #include "stdio.h" int inp(int a ...

  9. golang函数调用计时

    package main import ( "log" "time" ) func f() { defer timeoutCheck("f slow& ...

  10. 原生javascript Ajax

    代码 1. IE5 ,IE6 使用ActiveXObject对象,   其余现代浏览器都支持XMLHttpRequest对象: function ajaxObject(){ var xmlhttp; ...