题目:

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. (Medium)

分析:

开始想到的就是暴力搜索,但是超时了,所以考虑用动态规划来做。

符合单序列动态规划的特点,所以有

1. 状态:dp[i]表示前i个字符组成的子串可能的解码方式。

2. 初始化:dp[0] = 1, dp[1] = 1(s[0] != '0',因为没有0这个对应数字)

3. 递推关系

一般情况下:

if s[i - 1]与s[i -2]组成的在“1” ~“26”范围内, dp[i] = dp[i - 1] + dp[i - 2];

                   else, dp[i] = dp[i - 1];

但要注意的是,如果s[i - 1] == '0'需要特殊处理,当s[i - 2] == '1' || '2'时, dp[i] = dp[i - 2]

                       否则,则说明这个0没有对应位,这个字符串无法解码,直接返回0;

4.结果: dp[s.size()];

代码:

 class Solution {
public:
int numDecodings(string s) {
if (s.size() == ) {
return ;
}
int dp[s.size() + ];
dp[] = ;
if (s[] != '') {
dp[] = ;
}
for (int i = ; i <= s.size(); ++i) {
if (s[i - ] == '') {
if (s[i - ] == '' || s[i - ] == '') {
dp[i] = dp[i - ];
continue;
}
else {
return ;
}
}
if (s[i - ] == '' || (s[i - ] == '' && s[i - ] <= '') ) {
dp[i] = dp[i - ] + dp[i - ];
}
else {
dp[i] = dp[i - ];
}
}
return dp[s.size()];
}
};

2. 递推关系:

LeetCode91 Decode Ways的更多相关文章

  1. Leetcode91.Decode Ways解码方法

    一条包含字母 A-Z 的消息通过以下方式进行了编码: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 给定一个只包含数字的非空字符串,请计算解码方法的总数. 示例 1 ...

  2. [LeetCode] Decode Ways 解码方法

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

  3. [LintCode] Decode Ways 解码方法

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

  4. 44. Decode Ways && Gray Code

    Decode Ways A message containing letters from A-Z is being encoded to numbers using the following ma ...

  5. 91. Decode Ways

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

  6. leetcode@ [91] Decode Ways (Dynamic Programming)

    https://leetcode.com/problems/decode-ways/ A message containing letters from A-Z is being encoded to ...

  7. leetcode面试准备:Decode Ways

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

  8. [LeetCode] Decode Ways II 解码方法之二

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

  9. leetcode 639 Decode Ways II

    首先回顾一下decode ways I 的做法:链接 分情况讨论 if s[i]=='*' 考虑s[i]单独decode,由于s[i]肯定不会为0,因此我们可以放心的dp+=dp1 再考虑s[i-1] ...

随机推荐

  1. Leetcode414Third Maximum Number第三大的数

    给定一个非空数组,返回此数组中第三大的数.如果不存在,则返回数组中最大的数.要求算法时间复杂度必须是O(n). 示例 1: 输入: [3, 2, 1] 输出: 1 解释: 第三大的数是 1. 示例 2 ...

  2. 关于dex 64K 引用限制

    1.官方文档 https://developer.android.com/studio/build/multidex 主要内容: 什么是64K限制 编码时如何避免64K 限制 拆分dex避免64K 限 ...

  3. sass进阶

    代码的重用 基础的部分我们讲述了变量 Mixin 这两种方法可以增加扩展和重用 现在开始继续学习:extend继承 .class1 { border: 1px solid #ddd; } .class ...

  4. python 字符串的处理技巧--join

    >>> '+'.join('1234')'1+2+3+4'>>> '+'.join(a for a in '1234')'1+2+3+4'>>> ...

  5. python使用cPickle模块序列化实例

    python使用cPickle模块序列化实例 这篇文章主要介绍了python使用cPickle模块序列化的方法,是一个非常实用的技巧,本文实例讲述了python使用cPickle模块序列化的方法,分享 ...

  6. mysql 无法存储joda time的datetime类型

    com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect datetime value: '\xAC\xED\x00\x05sr\x ...

  7. jnhs-Myeclipse 10注册教程unable to access jarfile cracker.jar

    直接双击jar文件就可以 打开后,随便写一个名字 然后复制LICENSE_KEY的内容,打开myeclipse 在Code那里粘贴你刚才复制的内容,然后点击Save & Active Now ...

  8. Dockerfile Tomcat镜像制作

    FROM centos MAINTAINER taohaijun "thjtao@126.com" WORKDIR /home #上传安装包 COPY jdk-8u131-linu ...

  9. Linux 下安装和使用 Redis

    系统: CentOS-7-1611 首先安装gcc 因为要make编译 参照官网 wget  下载包 解压  make 编译完成后会在src目录下生成几个可执行文件 然后执行:make install ...

  10. RSA 2019安全大会:企业资产管理成行业新风向标,云上安全占优势

    美国时间3月4-8日,国际知名信息安全峰会RSA Conference在美国旧金山开幕,云安全及云可以为企业提供更可靠的资产管理方式成为大会热点. 此次峰会共吸引全球700多家机构参展,其中近42%为 ...