【一天一道LeetCode】#91. Decode Ways
一天一道LeetCode
本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github
欢迎大家关注我的新浪微博,我的新浪微博
欢迎转载,转载请注明出处
(一)题目
A message containing letters from A-Z is being encoded to numbers using the following mapping:
‘A’ -> 1
‘B’ -> 2
…
‘Z’ -> 26Given 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.
(二)解题
题目大意:1~26代表A~Z,给定一串数字,求出能解码出多少种不同的结果。
解题思路:采用动态规划,从后往前,状态转移方程是:dp[n] = dp[n+1]+dp[n+2].
例如:123,从后往前算,3一种,23两种,123就是1+2三种,
当然要考虑很多特殊情况,如230,227,012等等
class Solution {
public:
int numDecodings(string s) {
int len = s.length();
if(len==0) return 0;
vector<int> df(len,-1);
int num = dfsNumDecWays(s,0,df);
return num;
}
int dfsNumDecWays(string &s , int idx , vector<int>& df)
{
int num1= 0;
int num2=0;
if(idx>=s.length()){//越界了就返回1
return 1;
}
if(df[idx]!=-1) //代表没有访问到
{
return df[idx];
}
if(s[idx]>='1'&&s[idx]<='9') {
num1 = dfsNumDecWays(s,idx+1,df);//求dp[n+1]
if(idx+1<s.length())
{
int temp = (s[idx]-'0')*10+s[idx+1] -'0';
if(temp>=1&&temp<=26)
{
num2=dfsNumDecWays(s,idx+2,df);//求dp[n+2]
}
}
}
df[idx] = num1+num2;//状态转移方程
return num1+num2;
}
};
【一天一道LeetCode】#91. Decode Ways的更多相关文章
- leetcode@ [91] Decode Ways (Dynamic Programming)
https://leetcode.com/problems/decode-ways/ A message containing letters from A-Z is being encoded to ...
- Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理)
Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理) 题目描述 一条报文包含字母A-Z,使用下面的字母-数字映射进行解码 'A' -> 1 'B' -> 2 ...
- leetcode 91 Decode Ways I
令dp[i]为从0到i的总方法数,那么很容易得出dp[i]=dp[i-1]+dp[i-2], 即当我们以i为结尾的时候,可以将i单独作为一个字母decode (dp[i-1]),同时也可以将i和i-1 ...
- [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 ----- java
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#91 Decode Ways
原题地址 动态规划题,注意0导致的小陷阱. 代码: int numDecodings(string s) { ] < ] > ; ] >= ] <= : ; ; int nex ...
- [LeetCode] 639. Decode Ways II 解码方法 II
A message containing letters from A-Z is being encoded to numbers using the following mapping way: ' ...
- 【LeetCode】91. Decode Ways 解题报告(Python)
[LeetCode]91. Decode Ways 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...
随机推荐
- 分布式锁的几种使用方式(redis、zookeeper、数据库)
Q:一个业务服务器,一个数据库,操作:查询用户当前余额,扣除当前余额的3%作为手续费 synchronized lock db lock Q:两个业务服务器,一个数据库,操作:查询用户当前余额,扣除当 ...
- Linux中Mysql root用户看不到mysql库问题解决方式
第一种方式: 1.首先停止MySQL服务:service mysqld stop2.加参数启动mysql:/usr/bin/mysqld_safe --skip-grant-tables & ...
- Lintcode392 Is Subsequence solution 题解
[题目描述] Given a string s and a string t, check if s is subsequence of t. You may assume that there is ...
- 开源Spring解决方案--lm.solution
Github 项目地址: https://github.com/liumeng0403/lm.solution 一.说明 1.本项目未按java项目传统命名方式命名项目名,包名 如:org.xxxx. ...
- map函数用法详解
map函数是Python内置的高阶函数,它是一个典型的函数式编程例子.它的参数为: 一个函数function.一个或多个sequence.通过把函数function依次作用在sequence的每个元素 ...
- 转:Xming + PuTTY 在Windows下远程Linux主机使用图形界面的程序
一.原理Linux/Unix的X Window具有网络透明性.X Window系统里有一个统一的Server来负责各个程序与显示器.键盘和鼠标等输入输出设备的交互,每个有GUI的应用程序都通过网络协议 ...
- React 系列教程 1:实现 Animate.css 官网效果
前言 这是 React 系列教程的第一篇,我们将用 React 实现 Animate.css 官网的效果.对于 Animate.css 官网效果是一个非常简单的例子,原代码使用 jQuery 编写,就 ...
- Java并发编程(一)-为什么要并发
并发所带来的好处 1. 并发在某些情况(并不是所有情况)下可以带来性能上的提升 1) 提升对CPU的使用效率 提升多核CPU的利用率:一般来说一台主机上的会有多个CPU核心,我们可以创建多个线程,理论 ...
- Spring动态切换多数据源解决方案
Spring动态配置多数据源,即在大型应用中对数据进行切分,并且采用多个数据库实例进行管理,这样可以有效提高系统的水平伸缩性.而这样的方案就会不同于常见的单一数据实例的方案,这就要程序在运行时根据当时 ...
- 剑指Offer——知识点储备--Linux基本命令+Makefile
剑指Offer--知识点储备–Linux基本命令 1.linux下查看进程占用cpu的情况(top): 格式 top [-] [d delay] [q] [c] [S] [s] [i] [n] 主要参 ...