【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 ...
随机推荐
- JavaScript获取样式值的几种方法学习总结
本人经过整理总结出以下获取样式值的方法,如有错误请各位大佬指正. 有四种方法:style,currentStyle,getComputedStyle,rules 与 cssRules方法. 1. st ...
- tomcat jdk官网下载教程
Tomcat不同版本官网下载: 1.官网地址:http://tomcat.apache.org/ 2.点击要下载的版本进入下载页,点击Archives进入版本选择页,然后选择对应的版本文件夹,进去后点 ...
- Yii2 Working with Relational Data at ActiveDataProvider
Yii2 Working with Relational Data at ActiveDataProvider namespace common\models; use Yii; use yii\ba ...
- 使用kubeadm搭建Kubernetes集群
记录在石墨日记中,经常更新,懒得再复制了,直接点击下面链接查看吧 移步到此: https://shimo.im/docs/22WbxxQa1WUV9wsN/
- ASP.NET MVC网站学习问题积累(一)
最近工作压力比较大,不得已开始自学C#.同时网站开发业务开展迫在眉睫,只能先从ASP.NET学起.回想一下,连C#和ASP.NET的关系都没有明白,就被赶鸭子上架了...我觉得这将是我工作以来最具有戏 ...
- 如何使用cPanel管理域名和数据库
cPanel是一个基于web的基于web的控制面板,它简化了许多常见的系统管理任务,如网站创建.数据库部署和管理等.本指南向您展示了如何使用cPanel用户帐户管理域和数据库.所有这些指令都与位于端口 ...
- C++ 关于运算符重载
转载来源:http://c.biancheng.net/cpp/biancheng/view/216.html 重载运算符的函数一般格式如下: 函数类型 operator 运算符名称 (形参表列 ...
- UVALive 4731 Cellular Network(贪心,dp)
分析: 状态是一些有序的集合,这些集合互不相交,并集为所有区域.显然枚举集合元素是哪些是无法承受的, 写出期望的计算式,会发现,当每个集合的大小确定了以后,概率大的优先访问是最优的. 因此先对u从大到 ...
- UVA 11404 Plalidromic Subsquence (回文子序列,LCS)
最长回文子序列可以用求解原串s和反转串rv的LCS来得到,因为要求回文串分奇偶,dp[i][j]保存长度, 要求字典序最小,dp[i][j]应该表示回文子序列的端点,所以边界为单个字符,即i+j=le ...
- 2018.10.03 NOIP+ 模拟赛 解题报告
得分: \(30+5+0=35\)(考得真不咋滴) \(T1\):奥义商店(点此看题面) 以为很简单,对着这题想了一个多小时,最后果断打了个暴力交了... ... 看完题解发现其实也不是很难. 对于\ ...