A message containing letters fromA-Zis 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.

题意:给定编码信息,求有几种解码方式。

思路:动态规划,爬楼梯,词语打断、等一系列都可以使用动态规划。首先思考,若是字符串S为空,或者其首字母为'0'的情况,这时,因为编码是,数字是从1开始的,所以这两种情况下返回值都是0;当为字符串中间时,当前字符有两种编码方式,一是单独,二是和前面的字符组合解码;

1)当前面一个字符为'1'时,当前字符可以为0~9的任意一个,而为‘2’时,当前字符要小于'7',此时,当前字符既可以单独解码也可以和前面的联合起来解码dp[i]=dp[i-1]+dp[i-2];

2)若在字符串中间遇到'0',因为'0'能组成的只有两种'10'和'20',即,当前字符为'0'时,只能和前面的字母组合,即dp[i]=dp[i-2];

3)而前面的字符大于'2'时,当前字符只能是单独解码,即解码的方式在前一个的基础上不会增加dp[i]=dp[i-1]

所以,先根据当前字符是否为'0'先给dp[i]赋值:dp[i]=(s[i-1]=='0'?0:dp[i-1]):然后,若是满足条件一,则dp[i]+=dp[i-2];代码如下:

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

[LeetCode] decode ways 解码方式的更多相关文章

  1. [LeetCode] Decode Ways 解码方法

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

  2. [LeetCode] Decode Ways 解码方法个数、动态规划

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

  3. Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理)

    Leetcode 91. Decode Ways 解码方法(动态规划,字符串处理) 题目描述 一条报文包含字母A-Z,使用下面的字母-数字映射进行解码 'A' -> 1 'B' -> 2 ...

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

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

  5. [LeetCode] 91. Decode Ways 解码方法

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

  6. [LeetCode] Decode Ways(DP)

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

  7. [LintCode] Decode Ways 解码方法

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

  8. LeetCode:Decode Ways 解题报告

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

  9. [leetcode]91. Decode Ways解码方法

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

随机推荐

  1. vue渲染自定义json数据

    <template> <div class="wrap"> <div class="main"> <div class ...

  2. Vue 2.0 组件库总结

    UI组件 element - 饿了么出品的Vue2的web UI工具套件 Vux - 基于Vue和WeUI的组件库 mint-ui - Vue 2的移动UI元素 iview - 基于 Vuejs 的开 ...

  3. MySQL wait_timeout参数修改

    MySQL wait_timeout参数修改问题,可能经常会有DBA遇到过,下面就试验一下并看看会有什么现象. wait_timeout分为global级及session级别,如未进行配置,默认值为2 ...

  4. thinkphp phpmailer邮箱验证

    thinkphp 关于phpmailer的邮箱验证 一  . 登陆自己的邮箱,例如:qq邮箱.登陆qq邮箱在账户设置中开启smtp服务: 之后回发送一个授权码 , 这个授权码先保存下来,这个授权码在后 ...

  5. 带cookie请求数据

    经常会用到一些采集网上的资源,普通网站很好采,get_file_contents()/c_url(). 有的网站会有登陆后才能采集,需要带cookie请求获取(登陆网站相同方法),下面记录一下使用方法 ...

  6. ctf题目writeup(1)

    2019/1/28 题目来源:爱春秋 https://www.ichunqiu.com/battalion?t=1 1. 该文件是一个音频文件: 首先打开听了一下,有短促的长的....刚开始以为是摩斯 ...

  7. JS 实现随机验证码功能

    1.验证码 验证是网页常出现的一个验证点,所谓验证码类型有很多,下面代码只是实现一个简单的验证功能. <div> <input type = "text" id ...

  8. 3 python3 编码解码问题 upd接受数据

    1.python3下的中文乱码:send_data.encode("utf-8") from socket import * udp_socket = socket(AF_INET ...

  9. 复制MySQL数据库A到另外一个MySQL数据库B(仅仅针对innodb数据库引擎)

    方案一:(不用太大的变化my.ini文件) copy 原数据库A中的   数据库(database)  ib_logfile1  ib_logfile0   ibdata1: 关闭目的数据库B: 备份 ...

  10. leetcode笔记--2 reverse string

    my answer: 出错点:new_list[s] = list_s[u-1-s] 这样会出错, 重点:(1) map(str, s) 函数的使用,例:ls = [1,2,3]rs = map(st ...