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作为两个字母组合进行decode.
关键在于出现0的情况,当i-1为0的时候,我们不能将i和i-1进行组合,因为0不能作为前缀进行decode,此时dp[i]=dp[i-1],当i为0的时候,我们不能不能单独decode i,此时dp[i]=dp[i-2]
另外由于我们只需要保存前两步的状态,因此可以设置dp1,dp2作为dp[i-1],dp[i-2],每次计算完的时候更新即可
leetcode 639 Decode Ways II 链接
class Solution {
public:
int numDecodings(string s) {
int dp1=,dp2=,now;
if(s.length()==) return ;
if(s[]!='') dp1=;
for(int i=;i<s.length();i++){
now=s[i]!=''?dp1:;
if((s[i-]!='')&&((s[i-]-'')*+s[i]-''<=)) now+=i-<?:dp2; //要保证dp2存在
dp2=dp1;
dp1=now;
}
return dp1;
}
};
leetcode 91 Decode Ways I的更多相关文章
- 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 解码方法
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 ...
- 91. Decode Ways
题目: A message containing letters from A-Z is being encoded to numbers using the following mapping: ' ...
随机推荐
- 3.24网络攻防选拔题部分write up
20175221 3.24网络攻防选拔题部分write up Q1:百度一下,你就知道 解压题目得到一个文件夹和一个网址 打开文件夹,有三张图片 用winhex和stegsolve查看三张图片,没有 ...
- SP687 REPEATS - Repeats
给定字符串,求重复次数最多的连续重复子串. 题目很简单,被细节坑惨了... 前置的一个推论:请看这里. #include <bits/stdc++.h> using namespace s ...
- (二叉树 递归) leetcode 144. Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [1,null,2,3 ...
- Python变量命名规范
模块名: 小写字母,单词之间用_分割 ad_stats.py 包名: 和模块名一样 类名: 单词首字母大写 AdStats ConfigUtil 全局变量名(类变量,在java中相当于static变量 ...
- Python Pandas 简单使用之 API熟悉
1.read_csv li_index = ['round_id', 'index', 'c-sequen' ] dataset = pd.read_csv(file, low_memory=Fals ...
- 分布式监控系统开发【day38】:报警策略队列处理(五)
一.目录结构 二.报警策略队列处理 1.入口MonitorServer import os import sys if __name__ == "__main__": os.env ...
- Java实现AES加密
一)什么是AES? 高级加密标准(英语:Advanced Encryption Standard,缩写:AES),是一种区块加密标准.这个标准用来替代原先的DES,已经被多方分析且广为全世界所使用. ...
- 分享一个自搭的框架,使用Spring boot+Vue+Element UI
废弃,新的:https://www.cnblogs.com/hackyo/p/10453243.html 特点:前后端分离,可遵循restful 框架:后端使用Spring boot,整合了aop.a ...
- Centos 7 图形安装笔记(超详细)
1. 下载虚拟机(VMware Workstation Pro) 2. 安装虚拟机(Windows下安装虚拟机,自行网上搜索) 3. 下载Centos 7.4系统(国内建议使用阿里云: http:// ...
- python 写代码笔记 2017.6.15
其实并不是越复杂的代码越好,简单高效才是好. 关键是思路和逻辑,还有多看别人写的代码. 学习到了:)