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. android权限大全

    访问登记属性 android.permission.ACCESS_CHECKIN_PROPERTIES ,读取或写入登记check-in数据库属性表的权限 获取错略位置 android.permiss ...

  2. jvm

    -Xms128m表示JVM Heap(堆内存)最小尺寸128MB,初始分配-Xmx512m表示JVM Heap(堆内存)最大允许的尺寸256MB,按需分配. 说明:如果-Xmx不指定或者指定偏小,应用 ...

  3. [Math] 常见的几种最优化方法

    我们每个人都会在我们的生活或者工作中遇到各种各样的最优化问题,比如每个企业和个人都要考虑的一个问题“在一定成本下,如何使利润最大化”等.最优化方法是一种数学方法,它是研究在给定约束之下如何寻求某些因素 ...

  4. 【相当实用】如何让TortoiseSVN导出新增或修改过的文件

    当一个网站项目进入运营维护阶段以后,不会再频繁地更新全部源文件到服务器,这个时间的修改大多是局部的,因此更新文件只需更新修改过的文件,其他没有修改过的文件就没有必要上载到服务器.但一个稍微上规模的网站 ...

  5. Javascript闭包深入解析及实现方法

    1.什么是闭包 闭包,官方对闭包的解释是:一个拥有许多变量和绑定了这些变量的环境的表达式(通常是一个函数),因而这些变量也是该表达式的一部分.闭包的特点:1. 作为一个函数变量的一个引用,当函数返回时 ...

  6. JS学习:第一周——NO.1预解释

    1.何为预解释? 在当前作用域下,在JS代码执行之前,浏览器会对带var和带function的进行提前声明或定义: ①带var的:只声明不定义:告诉浏览器,有这么一个变量,但是并没有赋值 ②带func ...

  7. VS中批注的使用

    SAL 是 Microsoft 源代码注释语言. 使用源代码批注,可以使代码背后的意图更加清晰. 这些注释还可以使用自动化的静态分析工具更准确地分析代码,显著减少误判.那么什么是批注,举个简单的例子, ...

  8. 使用原生ajax处理json组成的数组

    和前一篇文章一样,直接上代码了,只是做个记录. 数据的提供页面,tigong.php <?php header("content-type:text/html;charset=utf- ...

  9. 学习android推荐网站

    1. Android Developers 作为一个Android开发者,官网的资料当然不可错过,从设计,培训,指南,文档,都不应该错过,在以后的学习过程中慢慢理解体会. 2. Android Gui ...

  10. 【Java EE 学习 34】【struts2学习第一天】

    一.struts2简介 struts2是一个用来开发MVC应用程序的框架.它提供了Web应用程序开发过程中的一些常见问题的解决方案. 1.struts2的作用域范围:三层架构当中的第一层,相当于MVC ...