【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 ...
随机推荐
- Struts2笔记1
一.简介 1.作用于web层:Struts2是一种基于MVC模式的轻量级Web框架; 2.各文件夹简介: apps:该文件夹存用于存放官方提供的Struts2示例程序,这些程序可以作为学习者 ...
- nmon 工具的使用
原文链接:https://www.ibm.com/developerworks/cn/aix/library/analyze_aix/ 引言 nmon 工具可以为 AIX 和 Linux 性能专家提供 ...
- SpringBoot的启动报错
1.
- 报错:Program "sh" not found in PATH
参考原文:http://vin-mail.blog.163.com/blog/static/37895280201211932919513/ 如果你按照我的方法 先配置了cygwin的环境变量,在打开 ...
- 真正理解 git fetch, git pull 以及 FETCH_HEAD(转)
转自http://www.cnblogs.com/ToDoToTry/p/4095626.html 真正理解 git fetch, git pull 要讲清楚git fetch,git pull,必须 ...
- Atom打造轻量化C/C++ IDE
写在前面 近期沉迷Atom的颜值无法自拔,在github的光环下,Atom凭借自身良好的素质,获得了大量开发者的青睐.随之而来的就是丰富的插件库,在插件帮助下,它对各种编程语言都有相当好的支持.对与一 ...
- python 多进程简单调用
python 多进程简 #!/usr/bin/env python #-*- coding:utf-8 -*- # author:leo # datetime:2019/5/28 10:03 # so ...
- Activiti学习记录(五)
1.排他网关 说明: 1) 一个排他网关对应一个以上的顺序流 2) 由排他网关流出的顺序流都有个conditionExpression元素,在内部维护返回boolean类型的决策结果. 3) 决策网关 ...
- GPT分区表的备份与恢复
GPT分区表的备份与恢复 keenshoes 2016-01-13 21:02:25 关键词: GPT, Partition, MBR,APPLE, GUID, Protective MBR 对于现 ...
- Uva 网络(Network,Seoul 2007,LA 3902)
#include<iostream> #include<cstring> #include<vector> using namespace std; +; int ...