题目:

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.

代码:

class Solution {
public:
int numDecodings(string s) {
if (s.empty()) return ;
if (s[]=='') return ;
const int n = s.size();
vector<int> dp(n+, );
dp[] = dp[] = ;
for ( int i=; i<=n; ++i )
{
if ( s[i-]=='' )
{
if ( s[i-]=='' || s[i-]>'')
{
return ;
}
else
{
dp[i] = dp[i-];
}
continue;
}
if ( s[i-]=='' || (s[i-]-'')*+s[i-]-''> )
{
dp[i] = dp[i-];
}
else
{
dp[i] = dp[i-] + dp[i-];
}
}
// for ( int i=0; i<dp.size(); ++i ){ cout << dp[i] << " "; }
// cout << endl;
return dp[n];
}
};

tips:

第一眼看到这道题是hard,AC率只有16%就觉得此题得非常困难;至少也得是个二维dp。结果就把自己给蒙住了。

就是常规思路,按照一维dp的路子往后走,当前元素可以自己单独解码或者跟前一个元素合起来被解码。

corner cases的核心在于两点:

1. 遇上0怎么办

2. 是否和大于26

基于这两个点,扫两次corner case就通过了。

上述的代码条件判断可以合并一些,但是为了保留原始思考痕迹,就保留原样了。

============================================

第二次过这道题,dp[0]=1一开始写成了dp[0]=0。

class Solution {
public:
int numDecodings(string s) {
if ( s.empty() ) return ;
int dp[s.size()+];
fill_n(&dp[], s.size()+, );
if ( s[]=='' ) return ;
dp[] = ;
dp[] = ;
for ( int i=; i<=s.size(); ++i )
{
if ( s[i-]=='' )
{
if ( s[i-]=='' || s[i-]>'' ){
return ;
}
else{
dp[i] = dp[i-];
}
continue;
}
if ( s[i-]=='' || (s[i-]-'')*+(s[i-]-'')> )
{
dp[i] = dp[i-];
}
else
{
dp[i] = dp[i-]+dp[i-];
}
}
return dp[s.size()]; }
};

【Decode Ways】cpp的更多相关文章

  1. hdu 4739【位运算】.cpp

    题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...

  2. Hdu 4734 【数位DP】.cpp

    题意: 我们定义十进制数x的权值为f(x) = a(n)*2^(n-1)+a(n-1)*2(n-2)+...a(2)*2+a(1)*1,a(i)表示十进制数x中第i位的数字. 题目给出a,b,求出0~ ...

  3. 【Climbing Stairs】cpp

    题目: You are climbing a stair case. It takes n steps to reach to the top. Each time you can either cl ...

  4. 【Valid Sudoku】cpp

    题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...

  5. 【Permutations II】cpp

    题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...

  6. 【Subsets II】cpp

    题目: Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...

  7. 【Sort Colors】cpp

    题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...

  8. 【Sort List】cpp

    题目: Sort a linked list in O(n log n) time using constant space complexity. 代码: /** * Definition for ...

  9. 【Path Sum】cpp

    题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...

随机推荐

  1. #include< >和#include“ ”的区别

    < >引用的是编译器的类库路径里面的头文件 " "引用的是你程序目录的相对路径中的头文件 假如你编译器定义的自带头文件引用在C:\Keil\c51\INC\下面 则#i ...

  2. nsight 中出现method could not be resolved 报错

    解决的方法就是现在编译选项中取消该报错. 项目右键->属性->c/c++常规->Code Analysis,选择"Use project settings"  中 ...

  3. Android中intent相关,setFlag(xx);

    intent.setFlags(参数)://参数用法如下 :FLAG_ACTIVITY_CLEAR_TOP: 例如现在的栈情况为:A B C D .D此时通过intent跳转到B,如果这个intent ...

  4. Cocos2d-x v3.1 Hello world程序(四)

    Cocos2d-x v3.1 Hello world程序(四) 在上一篇文章中我们我们已经使用Cocos-Console工具生成了工程,本机生成的目录为:"D:\CocosProject\T ...

  5. Visual Studio 2015简体中文版

    Visual Studio  2015简体中文版(企业版) http://download.microsoft.com/download/B/8/F/B8F1470D-2396-4E7A-83F5-A ...

  6. a low memory warning should only destroy the layer’s bitmap

    https://stablekernel.com/view-controller-in-ios-6/ Some of you may have noticed that your view contr ...

  7. iOS 制作表格 (数据源控制行,列数)

    记得去年面试的过程中,有一个面试官问我怎么制作表格.由于之前也没有做过,当时有点懵逼,今天想起来了,就用tableview制作了一个,望不要有人像我一样掉坑了, 直接上代码: // // ViewCo ...

  8. Hive 之元数据库的三种模式

    Hive 介绍 http://www.cnblogs.com/sharpxiajun/archive/2013/06/02/3114180.html Hive的数据类型和数据模型 http://www ...

  9. 利用Theano理解深度学习——Multilayer Perceptron

    一.多层感知机MLP 1.MLP概述 对于含有单个隐含层的多层感知机(single-hidden-layer Multi-Layer Perceptron, MLP),可以将其看成是一个特殊的Logi ...

  10. 记一次加密算法MD5

    通过MessageDigest可以获取到16个字节数组: MessageDigest md5 = MessageDigest.getInstance("MD5"); byte[] ...