题目链接:Decode Ways

  • 题目描述: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.


(1)思路:动态规划,寻找状态转移方程。

L为原始的由数字组成的字符串,函数f(x)表示在L中,从起点开始,长度为x的数字字符串能够表示为f(x)种字母字符串,L(x)表示为L字符串中第x个字符。

如果L(x)大于0,则f(x)至少为f(x - 1);

如果L(x - 1)为1,则f(x)至少为f(x - 2);

如果L(x - 1)为2且L(x) 小于等于6,则f(x)至少为f(x - 2);

(2)代码:

class Solution {public:int numDecodings(string s) {if (s.length() == 0 || s[0] == '0') {return 0;}int a = 1, b = 0, c = 0, x, y, z;for (auto each : s) {x = (each > '0') * a + b + (each <= '6') * c;y = (each == '1') * a;z = (each == '2') * a;a = x;b = y;c = z;}return a;}
};

(3)提交结果:

[LeetCode-Algorithms-91] Decode Ways (2017.10.19-WEEK7)相关推荐

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

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

  2. 【leetcode】91. Decode Ways A-Z的字母表示1-26的数字,反向破解多少种字符串的可能性...

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

  3. 【DFS + 记忆化递归 + DP】LeetCode 91. Decode Ways

    LeetCode 91. Decode Ways Solution1:我的答案 还是记录一下,最容易想到的是DFS,但是在第223/238个case上就超时了... class Solution { ...

  4. [LeetCode]91.Decode Ways

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

  5. 91 Decode Ways

    91 Decode Ways dp解法 O(1) space class Solution:# @param {string} s# @return {integer}def numDecodings ...

  6. 91 Decode Ways

    为什么我常常对做题产生恐惧,因为可能为了一个不算难的问题不知不觉绕进去2个小时,这显然是不值得的.这题就是如此. 还要注意,java && 的优先级高于||的优先级,而不是同级. pu ...

  7. LeetCode 91. Decode Ways

    问题链接 LeetCode 91 题目解析 A~Z对应数字1~26,给出一段数字串,求破译方法数. 解题思路 动态规划.关键在于分类,定义 \(dp[i]\) 为前i个字符的解密方法数,初始化为0. ...

  8. LeetCode 91 Decode Ways(编码方式)(*)

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

  9. leetcode 91. Decode Ways | 91. 解码方法(动态规划)

    题目 https://leetcode.com/problems/decode-ways/ 题解 dp 问题,首先用模拟的办法试一下,理清楚状态转移关系. 当走到 i 位置时,i 有两种选择: i 不 ...

最新文章

  1. keras训练完以后怎么预测_使用Keras建立Wide Deep神经网络,通过描述预测葡萄酒价格...
  2. 生成Gif动画缩略图-Gif动画水印的改进
  3. 实战:向GitHub提交代码时触发Jenkins自动构建
  4. CTFshow php特性 web95
  5. JSPatch Convertor 实现原理详解
  6. C/C++中extern关键字详解与应用
  7. boost::rational模块相关的测试程序
  8. bbPress 介绍,安装和中文包
  9. NoHttp开源Android网络框架1.0.0之架构分析
  10. DBATools PowerShell SQL Server数据库备份命令
  11. 【Android】自定义控件让TextView的drawableLeft与文本一起居中显示
  12. 关于USES_CONVERSION宏
  13. 苹果鼠标滚轮驱动_苹果鼠标magic mouse在戴尔电脑Windows10系统上使用滚轮的方法...
  14. “亲爱的程序员,我们不必害怕算法,flutterui库
  15. MySQL基础知识点集合
  16. 基于HSI颜色模型实现去除照片的“红眼现象”
  17. 【图像分类】基于PyTorch搭建LSTM实现MNIST手写数字体识别(单向LSTM,附完整代码和数据集)
  18. 第一个Flutter demo——实现无限循环列表
  19. 用python输出沙漏_sandglass(沙漏)——一个让人解脱的python时间处理库
  20. 听说要发年终奖了,来来来,我们互相伤害

热门文章

  1. fatal: No url found for submodule path ‘xxx‘ in .gitmodule
  2. 教你如何搭建自己的图床
  3. oracle外建什么作用,列举在数据库中外键的好处以及优劣
  4. QUANT[10]量化交易——因子暴露度,因子收益与模型
  5. c语言武汉市出租车收费标准 3公里以内,武汉的士/出租车起步价是多少?怎么算的?...
  6. 传统企业如何电子商务
  7. 2021全球与中国锂一次电池市场现状及未来发展趋势
  8. 数据分析项目-某加油站订单分析(SQL需求篇)
  9. [CodeForces908G]New Year and Original Order
  10. RHEL7-team双网卡绑定