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.

Solution:

Use Dynamic Programming method to solve the problem, for positition i, we can get that

if substring(i-1,i) is a valid string, the number of ways decoding from the beginning to position i equals the number of ways decoding from the beginning to position i-1;

if substring(i-2,i) is a valid string, the number of ways decoding from the beginning to position i equals the number of ways decoding from the beginning to position i-2;

 public class Solution {
public int numDecodings(String s) {
if(s==null||s.length()==0)
return 0;
int[] dp=new int[s.length()+1];
dp[0]=1;
if(isValid(s.substring(0,1))){
dp[1]=1;
}else{
dp[1]=0;
}
for(int i=2;i<=s.length();++i){
if(isValid(s.substring(i-1, i)))
dp[i]=dp[i-1];
if(isValid(s.substring(i-2, i))){
dp[i]+=dp[i-2];
} }
return dp[s.length()];
}
private boolean isValid(String s) {
// TODO Auto-generated method stub
int N=s.length();
if(N==1){
return(s.charAt(0)>='1'&&s.charAt(0)<='9');
}else if(N==2){
return((s.charAt(0)=='1'&&s.charAt(1)>='0'&&s.charAt(1)<='9')||
(s.charAt(0)=='2'&&s.charAt(1)>='0'&&s.charAt(1)<='6'));
}
return false;
}
}

[Leetcode] Decode Ways的更多相关文章

  1. [LeetCode] Decode Ways 解码方法

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

  2. [LeetCode] Decode Ways II 解码方法之二

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

  3. LeetCode:Decode Ways 解题报告

    Decode WaysA message containing letters from A-Z is being encoded to numbers using the following map ...

  4. [leetcode]Decode Ways @ Python

    原题地址:https://oj.leetcode.com/problems/decode-ways/ 题意: A message containing letters from A-Z is bein ...

  5. [LeetCode] Decode Ways(DP)

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

  6. [LeetCode] Decode Ways 解题思路

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

  7. [LeetCode] Decode Ways [33]

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

  8. [LeetCode] decode ways 解码方式

    A message containing letters fromA-Zis being encoded to numbers using the following mapping: 'A' -&g ...

  9. [LeetCode] Decode Ways 解码方法个数、动态规划

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

随机推荐

  1. UnknownHandler

    未知处理器 从struts2.1 开始 ,struts2配置文件的DTD中增加了<unknown-handler-stack…/>和<unknown-handler-ref…/> ...

  2. Backbone源码阅读手记

    Backbone.js是前端的MVC框架,它通过提供模型Models.集合Collection.视图Veiew赋予了Web应用程序分层结构.从源码中可以知道,Backbone主要分了以下几个模块: ( ...

  3. [译]ES6新特性:八进制和二进制整数字面量

    原文:http://whereswalden.com/2013/08/12/micro-feature-from-es6-now-in-firefox-aurora-and-nightly-binar ...

  4. 将.dat文件导入数据库

    *最近在搞文本分类,就是把一批文章分成[军事].[娱乐].[政治]等等. 但是这个先需要一些样本进行训练,感觉文本分类和"按图索骥"差不多,训练的文章样本就是"图&quo ...

  5. log4j的使用详细解析

    1 Log4j配置说明 1.1 配置文件Log4j可以通过java程序动态设置,该方式明显缺点是:如果需要修改日志输出级别等信息,则必须修改java文件,然后重新编译,很是麻烦: log4j也可以通过 ...

  6. Swift 3.0 【Swift 3.0 相较于 Swift 2.2 的变化】

    一.编译器和语法变化 函数或方法参数 调用函数或方法时从第一个参数开始就必须指定参数名 在Swift的历史版本中出现过在调用函数时不需要指定任何函数参数(或者从第二个参数开始指定参数名),在调用方法时 ...

  7. java生产者/消费者模式实现——一生产者一消费者(操作值)

    胶多不粘话多不甜,直接上代码: 生产者类: /** * Created by 51304 on 2016/2/28. */ public class P { private String lock; ...

  8. PHP常量详解:define和const的区别

    常量是一个简单值的标识符(名字).如同其名称所暗示的,在脚本执行期间该值不能改变(除了所谓的魔术常量,它们其实不是常量).常量默认为大小写敏感.通常常量标识符总是大写的. 可以用 define() 函 ...

  9. 支持向量机SVM

    SVM(Support Vector Machine)有监督的机器学习方法,可以做分类也可以做回归.SVM把分类问题转化为寻找分类平面的问题,并通过最大化分类边界点距离分类平面的距离来实现分类. 有好 ...

  10. C#高级编程笔记 Delegate 的粗浅理解 2016年9月 13日

    Delegate [重中之重] 委托 定义一:(参考)http://www.cnblogs.com/zhangchenliang/archive/2012/09/19/2694430.html 完全可 ...