题目:

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.

题解:

  这个题与之前的爬楼梯有些类似,只是有了0和26的限制判断。

Solution 1 ()

class Solution {
public:
int numDecodings(string s) {
if(s.empty() || s.size()> && s.front() == '') return ;
vector<int> dp(s.size()+, );
dp[] = ;
for(int i=; i<dp.size(); i++) {
if(s[i-] == '') dp[i] = ;
else dp[i] = dp[i-];
if(i> && (s[i-] == '' || s[i-] <= '' && s[i-] == ''))
dp[i] += dp[i-];
}
return dp.back();
}
};

Solution 2 ()

class Solution {
public:
int numDecodings(string s) {
if(s.empty() || s.front() == '') return ;
// r2: decode ways of s[i-2] , r1: decode ways of s[i-1]
int r1 = , r2 = ;
for(int i=; i<s.size(); i++) {
// zero voids ways of the last because zero cannot be used separately
if(s[i] == '') r1 = ;
// possible two-digit letter, so new r1 is sum of both while new r2 is the old r1
if(s[i-] == '' || s[i-] == '' && s[i] <= '') {
r1 = r2 + r1;
r2 = r1 - r2;
}
// one-digit letter, no new way added
else r2 = r1;
}
return r1;
}
};

【LeetCode】091. Decode Ways的更多相关文章

  1. 【LeetCode】91. Decode Ways 解题报告(Python)

    [LeetCode]91. Decode Ways 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...

  2. 【LeetCode】91. Decode Ways

    题目: A message containing letters from A-Z is being encoded to numbers using the following mapping: ' ...

  3. 【一天一道LeetCode】#91. Decode Ways

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 A messa ...

  4. 【LeetCode】241. Different Ways to Add Parentheses 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归构建所有表达式 方法二:分而治之 日期 ...

  5. 【LeetCode】241. Different Ways to Add Parentheses

    Different Ways to Add Parentheses Given a string of numbers and operators, return all possible resul ...

  6. 【LeetCode】394. Decode String 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 日期 题目地址:https://leetcode ...

  7. 【leetcode】394. Decode String

    题目如下: 解题思路:这种题目和四则运算,去括号的题目很类似.解法也差不多. 代码如下: class Solution(object): def decodeString(self, s): &quo ...

  8. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  9. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

随机推荐

  1. Spring Cloud(十三):Spring Cloud Sleuth服务链路追踪(zipkin)(转)

    这篇文章主要讲述服务追踪组件zipkin,Spring Cloud Sleuth集成了zipkin组件. 一.简介 Spring Cloud Sleuth 主要功能就是在分布式系统中提供追踪解决方案, ...

  2. FullPage.js 活动单页 - 全屏滚动插件

    插件描述:fullPage.js 是一个基于 jQuery 的插件,它能够很方便.很轻松的制作出全屏网站. https://www.uedsc.com/fullpage.html 官网 如今我们经常能 ...

  3. Hibernate demo之使用注解

    1.新建maven项目 testHibernate,pom.xml <?xml version="1.0" encoding="UTF-8"?> & ...

  4. Option可选值可选值(二)

    //: Playground - noun: a place where people can play import Cocoa var str1 = "供选链接和强制拆包的不同. &qu ...

  5. GC入门指南(二)------GC工作原理

    本系列博客旨在帮助大家理解java垃圾收集器及其工作原理,这是系列的第二篇. java垃圾回收事实上是由一个能够进行自己主动内存管理的进程完毕的,这使得程序猿在写代码的时候不必过多考虑内存释放与回收的 ...

  6. android菜鸟学习笔记5----第一个android程序

    程序功能:点击一个按钮,然后弹出一个提示信息 Step 1:在eclipse中新建一个android application project,在创建过程中不勾选create activity,这样就创 ...

  7. 九度OJ 1018:统计同成绩学生人数 (基础题)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:8807 解决:4651 题目描述: 读入N名学生的成绩,将获得某一给定分数的学生人数输出. 输入: 测试输入包含若干测试用例,每个测试用例的 ...

  8. kafka source type

    https://flume.apache.org/FlumeUserGuide.html # example.conf: A single-node Flume configuration # Nam ...

  9. ubuntu中设置wireshark抓包

    安装wireshark软件后,打开进行抓包的时候会提示权限不足.原因是普通用户没有执行权限,也打不开网络端口捕捉,因为dumpcap需要root权限. 产生这种问题的原因:比如:wireshark在进 ...

  10. 获取系统 SID

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/hadstj/article/details/26399533 获取系统 SID ((gwmi win ...