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. Java字节数组转按radix进制输出

    代码如下: public class Main_bytesToStr { public static void main(String[] args) throws IOException { // ...

  2. JavaScript中getBoundingClientRect()方法详解

    获取浏览器滚动的高度: scrollTop=document.documentElement.scrollTop || document.body.scrollTop getBoundingClien ...

  3. Linux命令--删除软连接

    1,建立软链接 ln -s 源文件 目标文件 例如:ln -s /usr/hb/ /home/hb_link 2,删除软链接 正确的是:rm -rf hb_link 错误的是:rm -rf hb_li ...

  4. Android检测网络是否正常代码!

    在Android开发中,如果该应用程序需要连接网络请求,那么最好我们先做一个检测网络是否在线的判断,否则程序容易出现卡死或FC等Bug,应该判断如果手机离线则弹出提示让用户检查网络,如果正常则继续执行 ...

  5. LCTT 三岁啦

    导读 不知不觉,LCTT 已经成立三年了,对于我这样已经迈过四张的人来说,愈发的感觉时间过得真快.这三年来,我们 LCTT 经历了很多事情,有些事情想起来仍恍如昨日. 三年前的这一天,我的一个偶发的想 ...

  6. [BZOJ1604][Usaco2008 Open]Cow Neighborhoods 奶牛的邻居

    [BZOJ1604][Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 试题描述 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发 ...

  7. python之路九

    paramiko paramiko模块是用python语言写的一个模块,遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接 ssh执行命令: import paramikossh = pa ...

  8. EXCEL科学计数法转为文本格式

    1.单元格格式-->特殊-->邮政编码 2.分列:选中数据-菜单栏“数据”-“分列”-下一步-下一步-选中文本-确定即可3.公式TEXT:如果数据在A列 =TEXT(A1,,0) 向下复制 ...

  9. C#基本工具代码

    1.下载Xlsx public static void TryToDisplayGeneratedFileXlsx(string writeFilePath, string fileName) { H ...

  10. python3 黑板客爬虫闯关游戏(二)

    第二关猜登录密码,需要用到urllib.request和urllib.parse 也很简单,给代码 import urllib.request as ur import urllib.parse as ...