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. Web jquery表格组件 JQGrid 的使用 - 11.问题研究

    系列索引 Web jquery表格组件 JQGrid 的使用 - 从入门到精通 开篇及索引 Web jquery表格组件 JQGrid 的使用 - 4.JQGrid参数.ColModel API.事件 ...

  2. Interop with Native Libraries

    http://www.mono-project.com/docs/advanced/pinvoke/

  3. IIS错误处理集合

    1.编译器错误消息: CS0016: 未能写入输出文件“c:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\ya ...

  4. Linux中挂载window7的共享文件

    window7主机: 设置要共享的文件夹 Linux Fedora: 0 su su root 1 samba-client yum install samba-client 2 cifs-utils ...

  5. Error: Collection was modified; enumeration operation may not execute.

    http://blog.csdn.net/ffeiffei/article/details/6131254

  6. PHP合并2个数字键数组的值

    先要了解一个基础知识点:PHP数组合并+与array_merge的区别分析 & 对多个数组合并去重技巧 <?php /** * PHP合并2个数字键数组的值 * * @param arr ...

  7. jdbctemplate中的批量更新使用,BigDecimal与造型的联系和区别

    //jdbctemplate批量新增的使用MENU_ID_LIST是前端页面传递到后端控制层,再由控制层传到实现层的List //JdbcTemplate是spring jdbctemplate通过注 ...

  8. JavaScript访问ab页面定时跳转代码

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. nginx 服务器重启命令,关闭

    nginx -s reload  :修改配置后重新加载生效 nginx -s reopen  :重新打开日志文件nginx -t -c /path/to/nginx.conf 测试nginx配置文件是 ...

  10. C#静态常量和动态常量的区别

    C#拥有两种不同的常量:静态常量(compile-time constants)和动态常量(runtime  constants).它们有不同的特性,错误的使用不仅会损失效率,还可能造成错误.相比之下 ...