【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 ...
随机推荐
- lintcode中等难度5道题
1.整数转罗马数字 对任一个罗马数字可以 由12个罗马字符进行加法操作完成,且大数在左,小数在右,可以将一个数字进行拆分来求解 2.买卖股票的最佳时机 II 可将问题转换为只要相连的两天prices[ ...
- EEC 欧姆龙PLC输入模块算法
Option Explicit Public MyArray(20000) As Integer Public MyArraySensor(20000) As Integer Sub 生成输入 ...
- 新增自定义聚合函数StrJoin
1.添加程序集Microsoft.SqlServer.Types CREATE ASSEMBLY [Microsoft.SqlServer.Types] AUTHORIZATION [sys] FRO ...
- LeetCode Valid Parentheses 有效括号
class Solution { public: void push(char c){ //插入结点 struct node *n=new struct node; n->nex=; n-> ...
- Nginx源码安装及调优配置(转)
导读 由于Nginx本身的一些优点,轻量,开源,易用,越来越多的公司使用nginx作为自己公司的web应用服务器,本文详细介绍nginx源码安装的同时并对nginx进行优化配置. Nginx编译前 ...
- Python F-string 更快的格式化
Python的格式化有%s,format,F-string,下面是比较这三种格式化的速度比较 In [12]: a = 'hello' In [13]: b = 'world' In [14]: f' ...
- python序列化(数据本地存放持久性存储)和反序列化
http://blog.csdn.net/uestcyao/article/details/7874817 #读取图片并存储为矩阵 from scipy.misc import imread im = ...
- Luogu P5008 逛庭院
题目传送门 我校神仙出的神仙题 \(\%\%\%\) 30分 找出所有有入度的点,排序,选前\(k\)个点,好了,30分到手. #include<iostream> #include< ...
- VS Code 用户自定义代码片段(React)
VS Code 用户自定义代码片段(React) .jsxReact组件模板:javascriptreact.json { "Import React": { "pref ...
- 手写promise
写在前面: 在目前的前端分开中,我们对于异步方法的使用越来越频繁,那么如果处理异步方法的返回结果,如果优雅的进行异步处理对于一个合格的前端开发者而言就显得尤为重要,其中在面试中被问道最多的就是对Pro ...