题目:

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.

代码(c++):

class Solution {public:int numDecodings(string s) {if(s[0] == '0') return 0;if(s.length() == 1) return 1;int decode1, decode2;decode1 = 1;if((s[0] == '1') || (s[0] == '2' && s[1]-'0' <= 6)){if(s[1] == '0') decode2 = 1;else decode2 = 2;}else if(s[1] == '0') return 0;else decode2 = 1;for(int i = 2; i < s.length(); i++){int temp = decode2;if((s[i-1] == '1') || (s[i-1] == '2' && s[i]-'0' <= 6)) {if(s[i] != '0') decode2 = decode1 * 2 + (decode2 - decode1);else decode2 = decode1;}else if(s[i] == '0') return 0;decode1 = temp;}return decode2;}
};

Leetcode91. Decode Ways相关推荐

  1. LeetCode91 Decode Ways

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

  2. [LeetCode] Decode Ways

    (Version 0.0) Decode Ways这道题从原理上说是一个比较简单的一维DP题目,用一个一维数组的元素dp[i] (i >= 1)来记录从头开始长度为i的substring有多少种 ...

  3. Decode Ways leetcode java

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

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

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

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

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

  6. [LeetCode]91.Decode Ways

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

  7. [LeetCode-Algorithms-91] Decode Ways (2017.10.19-WEEK7)

    题目链接:Decode Ways 题目描述:A message containing letters from A-Z is being encoded to numbers using the fo ...

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

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

  9. 91 Decode Ways

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

  10. 91 Decode Ways

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

最新文章

  1. Intel 的 MKL是可以用来训练的——官方的实验也提到了训练
  2. 【每周NLP论文推荐】 知识图谱重要论文介绍
  3. android测试之monkey测试
  4. 剑指offer の 1-10 之javascript实现
  5. 使用设计模式构建通用数据库访问类
  6. vue如何实现单页缓存方案分析
  7. pg数据库开启远程连接_Postgresql开启远程访问的步骤全纪录
  8. 《深入理解计算机系统》第七章 链接
  9. 将python源文件打包成exe文件
  10. kitti百度网盘分享 kitti百度云盘,全套kitti分享 自动驾驶
  11. 承接各类软件开发项目
  12. mysql 5.7基本优化_mysql5.7优化
  13. 渗透测试之信息收集漏洞库篇
  14. Skyler2003的资源QwQ
  15. 手游开发者大会交流OGEngine新版本发布
  16. oracle禁用系统用户登录,关闭系统 - 在 x86 平台上引导和关闭 Oracle Solaris
  17. 老猪带你玩转自定义控件三——sai大神带我实现ios 8 时间滚轮控件
  18. WebAPI 网站发布
  19. 北京信息科技大学第十三届程序设计竞赛暨ACM选拔赛(重现赛)题解
  20. linux yum安装redis

热门文章

  1. 三种图象处理的效率比较,用指针法最快
  2. 点击按钮后网页不向上滚动
  3. MVC 19个通信(请求)管道
  4. vi编辑器和系统分区
  5. Win7 + VS2015 + CMake3.6.1-GUI + Makefile 编译开源库
  6. 【转】如何通过事物代码查找对应BAPI
  7. Entity Framework 6 执行Linq to Entities异常p__linq__1 : String truncation: max=0, len=2, value='测试'...
  8. nginx图片缓存服务器配置实战
  9. MySql 安装 Win python3
  10. 图机器学习在度小满风控中的应用