【一天一道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 ...
随机推荐
- drools 手动创建kmoudle.xml文件
@Test public void test() { KieServices kieServices = KieServices.Factory.get(); KieResources resourc ...
- Angular 路由配置
路由,简单的来说就是让组件之间进行跳转和参数的传递. 1.先在app目录下创建一个名为app.route.ts的路由组件 2.打开app.route.ts 在里面创建路由组件的代码(可通过编辑器快捷生 ...
- Lintcode389 Valid Sudoku solution 题解
[题目描述] Determine whether a Sudoku is valid. The Sudoku board could be partially filled, where empty ...
- centos6 网卡配置,多IP设置
##云服务器 centos6网卡配置 #设置出口IP vim /etc/sysconfig/network-scripts/ifcfg-eth0DEVICE=seth0 #网卡名称 BOOTPROTO ...
- exp和imp的工作原理
--1.exp和imp的输入都是名字和值对: 如:exp parameter_name=value 或exp parameter_name=(value1,value2,value3..) --2.e ...
- html学习中
Html常用标签一 <body style="background-color:red;"> Body 标签 Style 属性 background-color:red ...
- PHP MySQL Order By 关键词
ORDER BY 关键词用于对记录集中的数据进行排序. ORDER BY 关键词 ORDER BY 关键词用于对记录集中的数据进行排序. ORDER BY 关键词默认对记录进行升序排序. 如果你想降序 ...
- 分布式服务框架Dubbo
随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,亟需一个治理系统确保架构有条不紊的演进. 单一应用架构 当网站流量很小时,只需一个应用, ...
- Docker容器时间与宿主机时间不一致
编写Dockerfile,在其中加入: RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \ && echo ' ...
- 设子数组A[0:k]和A[k+1:N-1]已排好序(0≤K≤N-1)。试设计一个合并这2个子数组为排好序的数组A[0:N-1]的算法。
设子数组A[0:k]和A[k+1:N-1]已排好序(0≤K≤N-1).试设计一个合并这2个子数组为排好序的数组A[0:N-1]的算法.要求算法在最坏情况下所用的计算时间为O(N),只用到O(1)的辅助 ...