[leetcode.com]算法题目 - Decode Ways
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) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
}
};
答案模板
本题情况很繁琐,尝试了好久才通过测试。注意“012”这样以零开头的string,number of ways 是0。代码如下:
class Solution {
public:
int numDecodings(string s) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int len = s.length();
if (==len || ''==s.at()) return ;
if (==len) return ;
if (==len){
int t1 = s.at()-'';
int t2 = s.at()-'';
t2 += t1*;
if( == t2 || == t2)
return ;
else if(t2<=)
return ;
else if(==t2%)
return ;
else
return ;
}
int *record = new int[len];
record[]=numDecodings(s.substr(len-,));
record[]=numDecodings(s.substr(len-,));
for(int k=;k<len;k++){
string s_string = s.substr(len-k-,k+);
int a = s_string.at()-'';
if (==a)
record[k]=;
else if (a>)
record[k]= record[k-];
else if (==a)
record[k]= record[k-]+record[k-];
else // (2==a)
{
int kk = s_string.at()-'';
if(kk>)
record[k]= record[k-];
else
record[k]= record[k-]+record[k-];
}
}
int result = record[len-];
delete[] record;
return result;
}
};
我的答案
注意分析其中的每一种情况,必须要都考虑周全。用record数组记录已经计算过的数据,避免用递归所产生的重复计算。
[leetcode.com]算法题目 - Decode Ways的更多相关文章
- 动态规划小结 - 一维动态规划 - 时间复杂度 O(n),题 [LeetCode] Jump Game,Decode Ways
引言 一维动态规划根据转移方程,复杂度一般有两种情况. func(i) 只和 func(i-1)有关,时间复杂度是O(n),这种情况下空间复杂度往往可以优化为O(1) func(i) 和 func(1 ...
- LeetCode之“动态规划”:Decode Ways
题目链接 题目要求: A message containing letters from A-Z is being encoded to numbers using the following map ...
- LeetCode(91) Decode Ways
题目 A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A ...
- [leetcode.com]算法题目 - Restore IP Addresses
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- [leetcode.com]算法题目 - Jump Game
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [leetcode.com]算法题目 - Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- [leetcode.com]算法题目 - Gray Code
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- [leetcode.com]算法题目 - Same Tree
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...
- [leetcode.com]算法题目 - Triangle
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
随机推荐
- JDBC连接MySql,配置url报错
使用JDBC连接MySql时出现:The server time zone value '�й���ʱ��' is unrecognized or represents more than one ...
- TCP的11种状态(转载)
TCP的11种状态 TCP三次握手建立连接 Tcp头部 六个标志位中,我们要用到三个: SYN:SYN= 1 表示这是一个连接请求或连接接受报文.在建立连接时用来进行同步序号(个人理解是,在建立连接的 ...
- C语言实现用位移运算符进行加减乘…
最近,在百度知道上回答问题,然后看见有的人问如何用位移运算符去进行加减乘除运算,于是巩固今天就在这总结一下. 先讲讲总体思路: 加法运算:将一个整数用二进制表示,其加法运算就是:相异(^)时, ...
- mybatis分页插件Mybatis_PageHelper 简单案例
源码地址(官网,文档) 使用条件: 支持mybatis 3.1.0+ sql 解析工具(jsqlparser.jar) 下载 Mybatis_PageHelper 版本随意,反正我用的5.0.0 m ...
- 2018.06.26 NOIP模拟 号码(数位dp)
题目背景 SOURCE:NOIP2015-GDZSJNZX(难) 题目描述 Mike 正在在忙碌地发着各种各样的的短信.旁边的同学 Tom 注意到,Mike 发出短信的接收方手机号码似乎都满足着特别的 ...
- Firefox,chrome,IE上传图片预览
首先判断IE或是Firefox,chrome.本文只测试了IE8中和Firefox,chrome是不一样的. 判断是否IE: if(-[1,]){//判断浏览器不是IE //alert((-[1 ...
- 使用Wireshark分析网络数据
一. Wireshark中查看TCP的三次握手和四次挥手: 上面的数据发送和接收两部分的info提示都是 [TCP segment of a reassembled PDU],网上的解释是TCP分片的 ...
- BZOJ 2005 [Noi2010]能量采集 (数学+容斥 或 莫比乌斯反演)
2005: [Noi2010]能量采集 Time Limit: 10 Sec Memory Limit: 552 MBSubmit: 4493 Solved: 2695[Submit][Statu ...
- Java潜在的坑持续总结
1.Java里如果有if (foo == 0),如果foo是null这里居然是会抛NPE异常而不是返回false: 2.Java里整形数值不能用==来比较,因为只有区间是[-128,127]的才能这么 ...
- OOP中的六种关系以及和JDK或框架中源码进行匹配对应
前言:这六种关系里:泛化=实现>组合>聚合>关联>依赖:其中组合-聚合-关联这三个如果只是给出一段代码是无法判断具体是什么关系的,需要配合语义或说业务场景来能进行区分(和设计模 ...