【Decode Ways】cpp
题目:
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的更多相关文章
- hdu 4739【位运算】.cpp
题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...
- 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~ ...
- 【Climbing Stairs】cpp
题目: You are climbing a stair case. It takes n steps to reach to the top. Each time you can either cl ...
- 【Valid Sudoku】cpp
题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...
- 【Permutations II】cpp
题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...
- 【Subsets II】cpp
题目: Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...
- 【Sort Colors】cpp
题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...
- 【Sort List】cpp
题目: Sort a linked list in O(n log n) time using constant space complexity. 代码: /** * Definition for ...
- 【Path Sum】cpp
题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...
随机推荐
- [转]linux C 打印当前时间
#include <stdio.h> #include <time.h> int main(void) { time_t t; time(&t); printf(&qu ...
- Thread.sleep 与Thread.currentThread.sleep
参考博客: https://blog.csdn.net/guangyinglanshan/article/details/51645053 公司项目近段时间要使用thread, 个人想去了解Threa ...
- w3cschool中jQuery测试结果总结
1.jQuery 是 W3C 标准. 2.$("div#intro .head") 选择器选取:class="intro" 的任何 div 元素中的首个 id= ...
- word2013标题编号变成黑框
在使用word2013时,之前正常的标题编号有部分变成了黑框 解决方法: 1.将光标移动到标题中黑框右侧 2.按动键盘上的左方向键,直到黑框变成灰色 3.同时按键盘 Ctrl+Shift+S键,弹出“ ...
- 24个节点测试Linux VPS/服务器速度一键脚本使用 附服务器配置
对于大部分网友而言,我们是希望购买的VPS.服务器既便宜也稳定,甚至还能提供更好的优质服务.这样的商家有没有呢?回答是基本没有.但是,只要我们购买的VPS在稳定性 和速度上对比同类的商家差不多,或者自 ...
- CRM User Status profile中Business Transaction字段的用途
有朋友问到User Status profile中Business Transaction字段的用途,如下图INPR, FINI所示. 实际上,这个字段作为一个桥梁,连接了User Status和Sy ...
- noip模拟赛#23
T1:n个元素的集合.要求取出k个子集,使得k个子集交集为空集.问有多少中取法. =>推了很久...想的是从k等于2的情况推到k等于3的情况....然后k=2推出来了k=3也推出来了...推了挺 ...
- 【BZOJ2730】[HNOI2012] 矿场搭建(找割点)
点此看题面 大致题意: 一张无向图,要求你在去掉任意一个节点之后,剩余的每个节点都能到达一个救援出口,问至少需要几个救援出口. 第一步:\(Tarjan\)求割点 首先,我们要跑一遍\(Tarjan\ ...
- 2017.12.9 Java中的排序---冒泡排序、快速排序、选择排序
//冒泡排序 public class demo{ public static void main(String[] args) { int[] sum={2,9,10,1,5,88}; System ...
- java基础——类加载与反射
第1章 类加载器 1.1 类的加载 当程序要使用某个类时,如果该类还未被加载到内存中,则系统会通过加载,连接,初始化三步来实现对这个类进行初始化. (1)加载 就是指将class文件读入内存,并为之创 ...