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. 一次 group by + order by 性能优化分析

    一次 group by + order by 性能优化分析 最近通过一个日志表做排行的时候发现特别卡,最后问题得到了解决,梳理一些索引和MySQL执行过程的经验,但是最后还是有5个谜题没解开,希望大家 ...

  2. css文本截字,超出文本省略号显示

    一.单行文本截字 p { text-overflow: ellipsis;/*显示省略号代替裁剪的文本*/ white-space: nowrap;/*空白处理方式 不换行*/ overflow: h ...

  3. FIFO队列(First In First Out)和优先队列

    queue<类型名> q; q.size() - 返回队列中元素个数 q.empty() - 若队列为空,返回true ,否则返回false q.pop() - 删除队首元素,但不返回其值 ...

  4. spfa专题

    SPFA专题 1通往奥格瑞玛的道路 在艾泽拉斯,有n个城市.编号为1,2,3,...,n. 城市之间有m条双向的公路,连接着两个城市,从某个城市到另一个城市,会遭到联盟的攻击,进而损失一定的血量. 每 ...

  5. VS中的快捷键

    1.代码中追踪函数的详细代码:   F12

  6. 两种方法实现Python二分查找算法

    两种方法实现Python二分查找算法   一. ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 arr=[1,3,6,9,10,20,30] def findnumber( ...

  7. LeetCode:21. Merge Two Sorted Lists(Easy)

    1. 原题链接 https://leetcode.com/problems/merge-two-sorted-lists/description/ 2. 题目要求 给出两个已经从小到大排序的链表ls1 ...

  8. babel配置

      首页 首页 首页 博客园 博客园 博客园 联系我 联系我 联系我 demo demo demo GitHub GitHub GitHub 管理 管理 管理 魔魔魔芋芋芋铃铃铃 [02]websto ...

  9. 创建react

    cnpm install -g create-react-app 安装项目create-recat-app myapp

  10. 汇编指令MOVSX与MOVZX

    MOVSX 操作数A ,操作数B MOVZX 操作数A ,操作数B 相同点:操作数B 空间必须小于 操作数A 1.格式与MOV基本相同 2.能完成小存储单元向大存储单元的数据传送 比如 movsx e ...