LeetCode OJ: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.
给一个数字,写出相应的解码方法,因为只需要给出解码的种数姐可以了,所以用dp比较简单就可以简单的解决了,答题的思路如下:
对于"123"来说,1只有一种,2有1, 2以及12两种,3 可以是 2自己的组合加上3 亦可以是23的组合加上1 自己的组合 ,一直类推,递推关系就找到了,代码如下所示:
class Solution {
public:
int numDecodings(string s) {
ret.resize(s.size());
if(!s.size()) return ;
for(int i = ; i < s.size(); ++i){
if(i > ){
string tmp = s.substr(i-, );
if(tmp >= "" && tmp <= "")
ret[i] += getRet(i - );
if(s[i] >= '' && s[i] <= '')
ret[i] += getRet(i - );
}else{
if(s[] >= '' && s[] <= '')
ret[i] = ;
}
}
return ret[s.size() - ];
} int getRet(int index)
{
if(index < )
return ret[];
else
return ret[index];
}
private:
vector<int> ret;
};
LeetCode OJ:Decode Ways(解码方法)的更多相关文章
- Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理)
Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理) 题目描述 一条报文包含字母A-Z,使用下面的字母-数字映射进行解码 'A' -> 1 'B' -> 2 ...
- [LeetCode] 91. Decode Ways 解码方法
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- [leetcode]91. Decode Ways解码方法
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- [LeetCode] Decode Ways 解码方法
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- [LeetCode] Decode Ways 解码方法个数、动态规划
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- [LintCode] Decode Ways 解码方法
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- 091 Decode Ways 解码方法
包含 A-Z 的字母的消息通过以下规则编码:'A' -> 1'B' -> 2...'Z' -> 26给定一个包含数字的编码消息,请确定解码方法的总数.例如,给定消息为 "1 ...
- Leetcode91.Decode Ways解码方法
一条包含字母 A-Z 的消息通过以下方式进行了编码: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 给定一个只包含数字的非空字符串,请计算解码方法的总数. 示例 1 ...
- [LeetCode OJ] Decode Ways
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- [LeetCode] 639. Decode Ways II 解码方法 II
A message containing letters from A-Z is being encoded to numbers using the following mapping way: ' ...
随机推荐
- python 内建函数isinstance的用法以及与type的区别
isinstance是Python中的一个内建函数 语法: isinstance(object, classinfo) 如果参数object是classinfo的实例,或者object是class ...
- 求CRC16校验
unsigned short DialogSerial::crc_ccitt(unsigned char *q,int len){ unsigned short ccitt_table[256] = ...
- Oh My Zsh 插件篇 - 实用工具
Oh My Zsh 除了为我们提供快捷的命令行操作之外,还提供了强大丰富的插件机制,每个社区贡献者都可以贡献自己的插件,让整个生态体系更加丰富完善.今天给大家介绍了一下它的实用工具类插件. 前面我们分 ...
- ubuntu下源码安装wget
1.背景 ubuntu18.04 64bit 2.安装方法如下: 2.1.获取源码 curl -o wget-1.20.tar.gz ftp://ftp.gnu.org/gnu/wget/wget-1 ...
- nodeJs和JavaScript的异同
JavaScript组成:ECMAScript(定义这门语言的基础,比如语法.数据类型.结构以及一些内置对象等).DOM(基于ECMASCRIPT,扩展出来的用于操作页面元素的方法).BOM(基于EC ...
- Linux基础知识点
1.目录结构 bin 存放二进制可执行文件(ls,cat,mkdir等) boot 存放用于系统引导时使用的各种文件 dev 用于存放设备文件 etc 存放系统配置文件 home 存放所有用户文件的根 ...
- 解题报告:codeforce 7C Line
codeforce 7C C. Line time limit per test1 second memory limit per test256 megabytes A line on the pl ...
- Python学习札记(二十一) 函数式编程2 map/reduce
参考:map/reduce Note 1.map():map()函数接收两个参数,一个是函数,一个是Iterable.map将传入的函数依次作用到序列的每个元素,并把结果作为新的Iterator返回. ...
- 生产消费模式:多线程读写队列ConcurrentQueue
需求:现需要将多个数据源的数据导入到目标数据库,这是一个经典的生产消费应用的例子. 直接上代码,看下实现: // 初始化列队缓冲区 队列大小为100 IDataCollection<List&l ...
- CountDownLatch详解
功能描述 一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待. 常见用法 多个人等一个信号后继续执行操作.例如5个运动员,等一个发令员的枪响. 一个人等多个人的信号. ...