题目:如下对应关系

'A' -> 1

'B' -> 2

...

‘Z’ -> 26

现在给定一个字符串,返回有多少种解码可能。例如:Given encoded message "12", it could be decoded as "AB" (1 2) or "L" (12).

思路:动态规划。

dp[i]表示s[0,...,i-1]的种数。

那么dp[i]和dp[i-1]存在什么关系呢。

我们设singleVal为dp[i-1]所有可能组合的最后一个数字,且该数字小于10,singleCnt为dp[i-1]的最后一个数字为小于10的种数。

例如对于“1323”的组合为:

1,3,2,3

13,2,3

1,3,23

13,23

这是singleVal为3,singleCnt为2,如果再加入一个4,只要在前面的基础上加一个逗号,再加加入的值

1,3,2,3,4

13,2,3,4

1,3,23,4

13,23,4

再考虑之前单一的数字和加入的数字是否可能组合,因为单一的val是3,加入的是4,组合后34是不合法的。所以此时dp[i] = dp[i-1].

如果我们上面说的单一的数和加入的数的组合是合法的话,dp[i] = dp[i-1] + singleCnt; 再更新singleCnt为dp[i-1], singleVal为新加入的值。

主体部分就确定了。

那么如果当前加入的数字为0,只有当singleCnt大于零,且singleVal<=2才可能有10和20的组合可能,否则就直接返回0.

class Solution {
public:
int numDecodings(string s)
{
int len = s.size();
if (len == || s[] == '') return ;
int singleVal = s[] - '';
int singleCnt = ;
vector<int> dp(len);
dp[] = ; for (int i = ; i < len; ++i)
{
if (s[i] == '')
{
if (singleCnt != && singleVal <= )
{
dp[i] = singleCnt;
singleCnt = ;
singleVal = ;
continue;
}
else
return ;
}
if (singleVal == || singleVal == && s[i] <= '')
{
dp[i] = dp[i - ] + singleCnt;
singleCnt = dp[i - ];
singleVal = s[i] - '';
}
else
{
dp[i] = dp[i - ];hh
singleCnt = dp[i - ];
singleVal = s[i] - '';
}
}
return dp[len - ];
}
};

如上是我的做法,我还看了别人的做法:

  • 初始条件:dp[0] = 1, dp[1] = (s[0] == '0') ? 0 : 1
  • dp[i] = ( s[i-1] == 0 ? 0 : dp[i-1] ) + ( s[i-2,i-1]可以表示字母 ? dp[i-2] : 0 ), 其中第一个分量是把s[0...i-1]末尾一个数字当做一个字母来考虑,第二个分量是把s[0...i-1]末尾两个数字当做一个字母来考虑

代码如下:

class Solution {
public:
int numDecodings(string s) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
//注意处理字符串中字符为0的情况
int len = s.size();
if(len == )return ;
int dp[len+];//dp[i]表示s[0...i-1]的解码方法数目
dp[] = ;
if(s[] != '')dp[] = ;
else dp[] = ;
for(int i = ; i <= len; i++)
{
if(s[i-] != '')
dp[i] = dp[i-];
else dp[i] = ;
if(s[i-] == '' || (s[i-] == '' && s[i-] <= ''))
dp[i] += dp[i-];
}
return dp[len];
}
};

leetcode[90] Decode Ways的更多相关文章

  1. leetcode@ [91] Decode Ways (Dynamic Programming)

    https://leetcode.com/problems/decode-ways/ A message containing letters from A-Z is being encoded to ...

  2. leetcode 91 Decode Ways I

    令dp[i]为从0到i的总方法数,那么很容易得出dp[i]=dp[i-1]+dp[i-2], 即当我们以i为结尾的时候,可以将i单独作为一个字母decode (dp[i-1]),同时也可以将i和i-1 ...

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

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

  4. [LeetCode] 639. Decode Ways II 解码方法 II

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

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

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

  6. leetcode 639 Decode Ways II

    首先回顾一下decode ways I 的做法:链接 分情况讨论 if s[i]=='*' 考虑s[i]单独decode,由于s[i]肯定不会为0,因此我们可以放心的dp+=dp1 再考虑s[i-1] ...

  7. 【leetcode】Decode Ways(medium)

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

  8. leetcode 91 Decode Ways ----- java

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

  9. [LeetCode OJ] Decode Ways

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

随机推荐

  1. ImageView建立selector在录音中遇到的小问题及解决方案

    随着两张照片做了一个selector,采用ImageView的src要么background采用selector当点击,总不会出现点击效果,这就是为什么?经过一番折腾,后来发现"揭秘&quo ...

  2. Codeforces 439C Devu and Partitioning of the Array(模拟)

    题目链接:Codeforces 439C Devu and Partitioning of the Array 题目大意:给出n个数,要分成k份,每份有若干个数,可是仅仅须要关注该份的和为奇数还是偶数 ...

  3. C# 获得Excel工作簿Sheet页面(工作表)集合的名称

    #region 获取Excel工作薄中Sheet页(工作表)名集合 /// <summary> /// 获取Excel工作薄中Sheet页(工作表)名集合 /// </summary ...

  4. CSS3添加属性选择: [attribute*=value] 、[attribute^=value] 和[attribute$=value]

    在CSS3新的 [attribute*=value] .[attribute^=value] 和[attribute$=value] 三个选择.使得属性选择使用通配符概念. 下面是利用这三个属性样本代 ...

  5. debugging python with IDLE

    1. start IDLE "Python 2.5"→"IDLE(Python GUI)" 2. open your source file window Fr ...

  6. SSIS从理论到实战,再到应用(3)----SSIS包的变量,约束,常用容器

    原文:SSIS从理论到实战,再到应用(3)----SSIS包的变量,约束,常用容器 上期回顾: SSIS从理论到实战,再到应用(2)----SSIS包的控制流   首先我们来看看包里面的变量 SSIS ...

  7. ClassLoader—流程观察程序执行类加载-verbose:class

    当调试器,有时你需要看到程序加载的类.记忆的恢复情况.本地接口调用,等等..这时候就需要-verbose命令. 在myeclipse能够通过右键设置(例如以下).也能够在命令行输入java -verb ...

  8. sql server 汉字的长度

    前几天改了人家程序中的一个小bug,就是输入时长度的校验问题.项目是.Net的,数据库是 sql server的.检查了一下,发现以前的人员把长度给控制小了,数据库中允许输入256的长度,而别人在as ...

  9. 经典算法题每日演练——第十一题 Bitmap算法

    原文:经典算法题每日演练--第十一题 Bitmap算法 在所有具有性能优化的数据结构中,我想大家使用最多的就是hash表,是的,在具有定位查找上具有O(1)的常量时间,多么的简洁优美, 但是在特定的场 ...

  10. maven+hudson构建集成测试平台

     1.下载hudson.war.2.命令行运行:java -jar hudson.war --httpPort=8070 -Dorg.eclipse.jetty.util.URI.charset=GB ...