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.

不用考虑开头是0的情况,题目不会给出这样的数据

这题是动态规划的题,找出递推公式比较难。
跟跳阶梯那个题挺像的。跳阶梯那个题的递推公式很好写出,这个题有点复杂,细节比较多。

思路就是用一个数组来存当前这个位置到最后的解码数。当然也可以使用几个变量。
这里从字符串后面往前推。

第i个元素处,往后的可能性,当第i个元素单独编码,就是1种可能,剩下的元素有dp[i+1]种可能;当第i和i+1个元素符合要求,剩下元素有dp[i+2]中可能。

大的公式就是:dp[i]=dp[i+1]+dp[i+2],这个前提是i元素自己符合要求(1,。9),而且i和i+1组成的数字也符合要求。当i和i+1组成的数字不符合要求时,dp[i]=dp[i+1]。这里没有考虑0的出现。当i出现0,dp[i]默认为0。因为它只有和前面的元素组成10,20才行,这样在前面i元素处,它的方法数就等于dp[i+2]

因为要有初始数据,所以dp数组的长度应该比字符串长度大一。初始值为1.,

class Solution {
public int numDecodings(String s) {
if(s==null||s.length()==0) return 0;
int len=s.length(); int[] dp=new int[len+1];
dp[len]=1;
dp[len-1]=s.charAt(len-1)!='0'?1:0;
for(int i=len-2;i>=0;i--){
if(s.charAt(i)=='0') continue;
else {
dp[i]=((Integer.parseInt(s.substring(i,i+2))<=26))?dp[i+1]+dp[i+2]:dp[i+1];
}
} return dp[0];
}
}

decode ways(动态规划)的更多相关文章

  1. Decode Ways——动态规划

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

  2. 91. Decode Ways(动态规划 26个字母解码个数)

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

  3. 动态规划小结 - 一维动态规划 - 时间复杂度 O(n),题 [LeetCode] Jump Game,Decode Ways

    引言 一维动态规划根据转移方程,复杂度一般有两种情况. func(i) 只和 func(i-1)有关,时间复杂度是O(n),这种情况下空间复杂度往往可以优化为O(1) func(i) 和 func(1 ...

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

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

  5. [LeetCode] Decode Ways 解码方法

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

  6. 44. Decode Ways && Gray Code

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

  7. leetcode面试准备:Decode Ways

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

  8. Decode Ways leetcode java

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

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

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

随机推荐

  1. Android初级教程进程间的通信AIDL

    在介绍跨程序进程间通信AIDL前,先看一下本程序activity与某个服务是怎么绑定在一起进行交互的. 需求:服务有两个方法.分别是播放音乐与停止播放音乐.该程序的活动要访问这两个方法,在activi ...

  2. Cocos2D iOS之旅:如何写一个敲地鼠游戏(八):为动画建立属性列表

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...

  3. 【一天一道LeetCode】#217. Contains Duplicate

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  4. ROS(indigo)国外开源示例包括多机器人控制等基于V-Rep和Gazebo的仿真

    ROS(indigo)国外开源示例包括多机器人的V-Rep和Gazebo仿真等 1 micros_swarm_framework 使用超级经典的stage. http://wiki.ros.org/m ...

  5. 扩展GDAL,支持CNSDTF格式(一)

    扩展GDAL,支持CNSDTF格式(一) 一.        简介 本文主要根据<中华人民共和国国家标准GB/T17798-2007--地理空间数据交换格式(Geospatialdata tra ...

  6. 小强的HTML5移动开发之路(16)——神奇的拖放功能

    来自:http://blog.csdn.net/dawanganban/article/details/18181273 在智能手机发展飞速的现在拖放功能已经成为一种时尚,但是在我们的浏览器上是不是还 ...

  7. 【一天一道LeetCode】#63. Unique Paths II

    一天一道LeetCode (一)题目 Follow up for "Unique Paths": Now consider if some obstacles are added ...

  8. 浅谈OC内存管理

    一.基本原理 (一)为什么要进行内存管理. 由于移动设备的内存极其有限,所以每个APP所占的内存也是有限制的,当app所占用的内存较多时,系统就会发出内存警告,这时需要回收一些不需要再继续使用的内存空 ...

  9. Chipmunk碰撞形状:cpShape

    目前有3种碰撞类型: 圆(Circles):最快并且最简单的碰撞形状 线段(Line segment):主要用于静态形状.可以表示斜线(Can be beveled in order to give ...

  10. [转]Android长度单位详解

    android中定义的dimension单位有以下这些:px(Pixels ,像素):对应屏幕上的实际像素点.in(Inches ,英寸):屏幕物理长度单位.mm(Millimeters ,毫米):屏 ...