典型的动态规划题,和《剑指offer》中的矩形覆盖题相似,只不过要考虑不同的边界情况。

另外对于《剑指offer》中的矩形覆盖题,值得考虑的是当矩形是n*n时,会有多少种覆盖的情况?

 1 class Solution {
 2 public:
 3     int numDecodings(string s) {
 4         if(s==""||s[0]=='0')
 5             return 0;
 6         if(s.length()==1&&s[0]!='0')
 7             return 1;
 8         if(s.length()>=2&&s[1]=='0')
 9         {
10             if((s[0]-'0')*10+s[1]-'0'>20)
11                 return 0;
12         }
13         int len=s.length();
14         vector<int> iv(len,0);
15         int flag=0;
16         if(s[0]!='0'&&s[1]!='0')
17         {
18             iv[0]=1;iv[1]=1;
19             if((s[0]-'0')*10+s[1]-'0'<=26)
20                 iv[1]++;
21         }
22         else if(s[0]!='0'&&s[1]=='0')
23         {
24             iv[0]=1;iv[1]=1;flag=1;
25         }
26         for(int i=2;i<len;i++)
27         {
28             if(s[i]=='0')
29             {
30                 if(flag==1)
31                     return 0;
32                 if((s[i-1]-'0')*10+s[i]-'0'>20)
33                     return 0;
34                 iv[i]=iv[i-2];
35                 flag=1;
36             }
37             else
38             {
39                 if(flag==1)
40                 {
41                     iv[i]=iv[i-1];
42                 }
43                 else
44                 {
45                     iv[i]=((s[i-1]-'0')*10+s[i]-'0'<=26)?(iv[i-1]+iv[i-2]):iv[i-1];
46                 }
47                 flag=0;
48             }
49         }
50         return iv[len-1];
51     }
52 };

View Code

转载于:https://www.cnblogs.com/vaecn/p/5346293.html

LC91 Decode Ways相关推荐

  1. [LeetCode] Decode Ways

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

  2. Decode Ways leetcode java

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

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

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

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

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

  5. [LeetCode]91.Decode Ways

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

  6. [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 ...

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

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

  8. 91 Decode Ways

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

  9. 91 Decode Ways

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

最新文章

  1. PyQt5 技术篇-设置输入框的placeholder方法,Qt Designer设置Line Edit、Text Edit编辑框的placeholder
  2. linux tar.gz文件安装
  3. asp.net 日期转换
  4. 【CodeForces - 633D】Fibonacci-ish (离散化,暴力枚举+STPmap,fib数列收敛性质)
  5. loading 遮罩demo
  6. php更改tittle,phpcms V9修改默认title标签
  7. 【javascript】ajax跨域post传值
  8. [Python图像识别] 四十五.目标检测入门普及和ImageAI“傻瓜式”对象检测案例详解 (1)
  9. webService接口调用
  10. android请求网络接口(xutils3进行网络请求)
  11. 刷完 900 多道算法题的首次总结:LeetCode 应该怎么刷?
  12. java 真少林倚天屠龙夺艳记,2019年9月13日資訊重點:月夜吟詠詩意長安十九屆中央第四輪巡視全部進駐...
  13. 2022年软件测试有哪些趋势?
  14. 《数据库系统概论》3.3.1模式的定义与删除实操
  15. 【企业管理】北邮国际学院大二上期末复习
  16. 怎么在html显示已登录状态,登录成功后,返回时不再显示登录等已经没用的页面...
  17. ABAP--新语法--Open SQL--第二天-- Built-In Functions内置方法
  18. Axure RP8 8.1.0.3381 安装包、注册码 激活码
  19. 美团技术团队-日志导致线程Block的这些坑,你不得不防
  20. 计算机分隔线教程,计算机CAD画线教程

热门文章

  1. matlab进行fm调制与解调,基于matlab的fm信号调制与解调.doc
  2. C语言之指针总结(1)
  3. 为什么现在90后员工离职率那么高?
  4. Matplotlib数据可视化之堆叠图、饼图(plt.stackplot\plt.pie)
  5. mel表达式_常用表达式Mel的用法-李英江-
  6. fix indent命令实现纳米摩擦及摩擦力计算案例
  7. 大数据——云服务常用词汇及含义
  8. 9月14日更新的sublime激活码
  9. Ghost的一些使用方法
  10. flow hive 新型蜂箱_流动的蜂蜜盛宴:Flow Hive蜂箱获数百万美元众筹款