题目

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.

分析

代码

/*---------------------------------------
*   日期:2015-06-23
*   作者:SJF0115
*   题目: 91.Decode Ways
*   网址:https://leetcode.com/problems/decode-ways/
*   结果:AC
*   来源:LeetCode
*   博客:
-----------------------------------------*/
#include <iostream>
using namespace std;class Solution {
public:int numDecodings(string s) {int size = s.size();if(s[0] == '0'){return 0;}//ifif(size == 0 || size == 1){return size;}//ifint pre = 1,cur = 1,res;for(int i = 1;i < size;++i){if(isValid(s[i-1],s[i]) && isValid(s[i])){res = pre + cur;}//ifelse if(!isValid(s[i-1],s[i]) && isValid(s[i])){res = cur;}//elseelse if(isValid(s[i-1],s[i]) && !isValid(s[i])){res = pre;}//elseelse{return 0;}//elsepre = cur;cur = res;}//forreturn res;}
private:bool isValid(char pre,char cur){if(pre == '1' || (pre == '2' && cur <= '6')){return true;}//ifreturn false;}bool isValid(char cur){if(cur >= '1' && cur <= '9'){return true;}//ifreturn false;}
};int main(){Solution s;string str("1202111110");cout<<s.numDecodings(str)<<endl;return 0;
}

运行时间

思路2–超时

/*---------------------------------------
*   日期:2015-06-21
*   作者:SJF0115
*   题目: 91.Decode Ways
*   网址:https://leetcode.com/problems/decode-ways/
*   结果:超时
*   来源:LeetCode
*   博客:
-----------------------------------------*/
#include <iostream>
using namespace std;class Solution {public:int numDecodings(string s) {int size = s.size();if(size == 0 || size == 1){return size;}//ifint index = -1;int count = 0;helper(s,index,count,"");return count;}
private:void helper(string &s,int index,int &count,string word){int size = s.size();if(index == size-1){++count;cout<<"word->"<<word<<endl;return;}//if// 步长为1或者2int num = 0;for(int i = 1;i <= 2;++i){if(index + i < size){num = num * 10 + s[index+i] - '0';if(num <= 26 && num > 0){word += ('A'+num-1);helper(s,index+i,count,word);word.erase(word.size()-1);}//ifelse{break;}}//if}//for}
};int main(){Solution s;string str("1234");cout<<s.numDecodings(str)<<endl;return 0;
}

[LeetCode]91.Decode Ways相关推荐

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

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

  2. LeetCode 91. Decode Ways

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

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

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

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

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

  5. [dp] LeetCode 91. Decode Ways

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

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

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

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

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

  8. 91 Decode Ways

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

  9. 91 Decode Ways

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

最新文章

  1. 独家 | 如何用XGBoost做时间序列预测?
  2. 怎样才算熟悉python-怎样才算python入门
  3. 微信 WEUI 的 switch button 精简提取
  4. python接口自动化(十四)--session关联接口(详解)
  5. Arduino 笔记。开篇
  6. 学python编程_学习Python编程,我们应该如何学?学习内容包括哪些?
  7. SpringBoot指南(一)——SpringBoot入门
  8. 程序员面试金典——18.1另类加法
  9. 宋体、代码-iOS网络编程实践--NSStream实现TCP Socket iPhone客户端-by小雨
  10. ora-01031:insufficient privileges解决方法总结
  11. mysql安装_win版
  12. jdk8,jdk10,jdk12新特性
  13. 豆子特斯拉,豆箕宁德时代
  14. 实现将html网页中的元素复制到微信编辑器中,并正常呈现排版
  15. vant-ui的官方入口
  16. 【DuiLib入门基础】九宫格corner属性详细解释
  17. 如何以聪明的方式提问
  18. Django项目(sysinfo系统信息和用户信息展示)
  19. python从入门到弃坑中子弹部分的问题
  20. Vue结合element-ui实现导航菜单展开收缩小功能

热门文章

  1. ubuntu 调整cpu运行模式至高性能
  2. 用表格做出的阴影扇形图
  3. 安徽理工大学计算机研究生学院,计算机学院第二届研究生学术论坛圆满闭幕
  4. dellR720xd服务器做raid安装操作系统
  5. 【转】前端图片该保存为什么格式?png or jpg?
  6. STM32中的程序在RAM还是FLASH里运行?
  7. Caesar密码的生成与破解
  8. ZOU YI BU
  9. Week 7 - Distributional Representations(分布表示)
  10. 1.Transformer-Attention is all your need论文详读-PartⅠ(摘要、引言、背景)