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: ' ...
随机推荐
- 带你走进AJAX(1)
ajax是什么? (1)ajax (asynchronouse javascript and xml) 异步的javascript 和xml (2)ajax是一个粘合剂,将javascript.xml ...
- intellij-idea+maven搭建scala环境
一 . 安装JDK 1. 下载地址: http://download.oracle.com/otn-pub/java/jdk/8u181-b13/96a7b8442fe848ef90c96a2fad6 ...
- php 输出 sql语句
第一种方法 $data = M('news')->field("title,date_format(postdate,'%Y-%m-%d') as postdate,content&q ...
- jqgrid动态填充select
前两天使用jquery动态填充jqgrid的select,默认随便用一个dict填充(初始化页面是会替换掉),用着可以,后来发现当行数增多,回到以前行继续编辑发现select又变成了那个用来避免报错随 ...
- JS类、对象、方法、prototype、_proto_
案例代码: function People(name) { //对象属性 this.name = name; //对象方法 this.Introduce = function() { alert(&q ...
- go基础语法
定义变量: 可放在函数内,或直接放在包内使用var集中定义变量使用:=定义变量写的短一些 package main import ( "fmt" "math" ...
- Linux下C连接MySql数据库
目录: 一.解决小的问题: 二.大问题,如果你不小心把/usr/lib的所属用户改了导致sudo命令用不了: 三.C连接MySql编程本身: 其实写这个程序真的很简单,十多分钟的事情,只是以前没在Li ...
- 【读书笔记】《深入浅出nodejs》第四章 异步编程
1. 异步编程的基础 -- 函数式编程 (1)高阶函数 -- 是可以把函数作为参数,或是将函数作为返回值的函数. (2)偏函数用法 -- 创建一个调用另外一个部分 -- 参数或变量已经预置的函数 -- ...
- Spring中Bean生命周期
Spring中的bean生命周期是一个重要的点,只有理解Bean的生命周期,在开发中会对你理解代码是非常有用的.对于Bean的周期,个人认为可以分为四个阶段.第一阶段:Bean的实例化,在该阶段主要是 ...
- Docker Mysql主主同步配置搭建Demo
主主同步配置和主从配置很相似,仅需稍做修改就可以了,对主从配置有疑问可以查看 上一篇文章. 进行Docker操作前,先建立目录,我的路径是d:/docker/mysql,目录结构如下: --mysql ...