题目地址:


LeetCode 动态规划(Dynamic programming)系列题目:LeetCode 动态规划(Dynamic programming)系列题目


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

'A' -> 1
'B' -> 2
...
'Z' -> 26

Given a non-empty string containing only digits, determine the total number of ways to decode it.

Example 1:

Input: "12"
Output: 2
Explanation: It could be decoded as "AB" (1 2) or "L" (12).

Example 2:

Input: "226"
Output: 3
Explanation: It could be decoded as "BZ" (2 26), "VF" (22 6), or "BBF" (2 2 6).

python的动态规划解法:

class Solution:def numDecodings(self, s):dpi_1=0dp_i=int(s>'')before=''for d in s:dpi_1, dp_i, before = dp_i, (d>'0')*dp_i + (9<int(before+d)<27)*dpi_1, dreturn dp_i

java的解法:

public class Solution {public int numDecodings(String s) {if(s == null || s.length() == 0) {return 0;}int n = s.length();int[] dp = new int[n+1];dp[0] = 1;dp[1] = s.charAt(0) != '0' ? 1 : 0;for(int i = 2; i <= n; i++) {int first = Integer.valueOf(s.substring(i-1, i));int second = Integer.valueOf(s.substring(i-2, i));if(first >= 1 && first <= 9) {dp[i] += dp[i-1];  }if(second >= 10 && second <= 26) {dp[i] += dp[i-2];}}return dp[n];}
}

LeetCode 91. Decode Ways--动态规划DP的Python和Java解法相关推荐

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

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

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

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

  3. [LeetCode]91.Decode Ways

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

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

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

  5. LeetCode 91. Decode Ways

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

  6. [dp] LeetCode 91. Decode Ways

    输入:一个字符串,只包含0-9的字符. 输出:解码种类 规则:有一种信息映射规则 A->1,B->2-Z->26. 例如输入'1',只能解码为A. 输入'12',可以解码为'AB', ...

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

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

  8. 【重点!DP】LeetCode 639. Decode Ways II

    LeetCode 639. Decode Ways II 参考网址:https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-639-d ...

  9. 91 Decode Ways

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

最新文章

  1. Cant find model en_core_web_sm
  2. [LeetCode] NO. 100 Same Tree
  3. Softmax的理解与应用
  4. python版本选择-【小白学python】之一:版本选择
  5. 交通银行签约第四范式,建设全行级统一AI能力平台
  6. c语言编程计算人口增长模式转变示意图,读“人口增长模式及其转变示意图”,回答下列问题。(5分)(1)图中字母代表的人口增长模式是:A____________、B____...
  7. NCRE四级网络工程师考题详解----目录分解法
  8. 无盘服务器回写盘intel,无盘回写盘碎片清理工具 完美解决无盘回写盘碎片
  9. RocketMQ架构
  10. QEMU CVE-2020-14364 漏洞分析(含 PoC 演示)
  11. 你所不知道的Quartz特性
  12. f分布表完整图a=0.01_SQL数据库完整性
  13. 一份让你效率翻倍的年终总结
  14. matlab虚数求模,matlab计算带有复数的函数,最后求复数函数的模,结果里面却有...
  15. 计算机硬盘换,电脑硬盘可以随便换吗
  16. excel smart流程图增加_Excel也能做出世界级的流程图,简单好用,我用1分钟就画好了...
  17. matlab dff求导,matlab的多元函数微积分学.ppt
  18. 亚马逊测评项目怎么做?市场如何?测评资源怎么找​?
  19. BLDC反电势过零检测计算
  20. 使用局域网IP地址作为小程序的测试IP

热门文章

  1. RDKit | 基于随机森林(RF)的机器学习模型预测hERG阻断剂活性
  2. 第三十一课.矩阵胶囊与EM路由
  3. Android中获取手机的IMEI
  4. 可视化生信分析利器 Galaxy 之 Docker 部署
  5. 易生信-扩增子教程02-真菌引物选择
  6. linux挂载分区失败,Ubuntu分区挂载错误与Grub引导错误的修复
  7. mysql内存爆_线上MySQL机器内存爆掉原因分析与解决
  8. R语言使用ggplot2包使用geom_density()函数绘制分组密度图(改变图例位置、移除图例)实战(density plot)
  9. R创建哑变量(Dummy Variables)
  10. 梯度爆炸是什么?有什么后果?如何判断梯度爆炸?如何避免梯度爆炸?