题目

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.

原题链接(点我)

解题思路及代码

解码方法数量问题。

英文26个字母相应1到26,给一串数字。问翻译为字母有多少种方法?

这个题第一思路是想到使用组合排列的方法,穷举全部的可能。非常好。写出例如以下代码

class Solution {
public:
int numDecodings(string s) {
int count = 0;
helper(0, s, count);
return count;
} void helper(int start, const string& s, int& count){
if(start == s.size()){
++count;
return;
}
int key=0;
for(int i=start; i<s.size(); ++i){
if(s[start] == '0') return;
key = 10*key + s[i] - '0' ;
if(key>26) return;
helper(i+1, s, count);
}
}
};

可是提交后出来的结果是超时。

再想想,使用动态规划的方法来做。

对于串s[0...i]的解码数量应该和s[0...i-1], s[0...i-2]的解码数量有关系。

dp[i]: 代表s[0...i-1]的解码数量,

dp[i] = { (s[i-1]!='0')?dp[i-1]:0 } + { s[i-2...i-1]<='26' ? dp[i-2] : 0 } ;

代码例如以下:

class Solution {
public:
int numDecodings(string s) {
int n = s.size();
if( n<=0 || s[0]=='0') return 0;
vector<int> dp(n+1, 0);
dp[1] = dp[0] = 1;
for(int i=2; i<=n; ++i){
if(s[i-1] != '0') dp[i] = dp[i-1];
if(s[i-2]=='1' || (s[i-2]=='2'&&s[i-1]<'7'))
dp[i] += dp[i-2];
}
return dp[n];
} };

上述动态规划优化后能够仅仅使用3个变量而不是一个数组。代码例如以下:

class Solution {
public:
int numDecodings(string s) {
if(s.size()<=0 || s[0]=='0') return 0;
int cur=0, cur_1 = 1, cur_2 = 1;
for(int i=2; i<=s.size(); ++i){
if(s[i-1] != '0') cur += cur_1;
if(s[i-2]=='1' || (s[i-2]=='2'&&s[i-1]<'7'))
cur += cur_2;
cur_2 = cur_1, cur_1 = cur, cur = 0;
}
return cur_1;
}
};

假设你认为本篇对你有收获,请帮顶。

另外,我开通了微信公众号--分享技术之美。我会不定期的分享一些我学习的东西.
你能够搜索公众号:swalge 或者扫描下方二维码关注我

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc3dhZ2xl/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

(转载文章请注明出处: http://blog.csdn.net/swagle/article/details/30231807
)

[LeetCode] Decode Ways [33]的更多相关文章

  1. [LeetCode] Decode Ways 解码方法

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

  2. [LeetCode] Decode Ways II 解码方法之二

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

  3. LeetCode:Decode Ways 解题报告

    Decode WaysA message containing letters from A-Z is being encoded to numbers using the following map ...

  4. [leetcode]Decode Ways @ Python

    原题地址:https://oj.leetcode.com/problems/decode-ways/ 题意: A message containing letters from A-Z is bein ...

  5. [Leetcode] Decode Ways

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

  6. [LeetCode] Decode Ways(DP)

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

  7. [LeetCode] Decode Ways 解题思路

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

  8. [LeetCode] decode ways 解码方式

    A message containing letters fromA-Zis being encoded to numbers using the following mapping: 'A' -&g ...

  9. [LeetCode] Decode Ways 解码方法个数、动态规划

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

随机推荐

  1. HDU 2722 Here We Go(relians) Again

    最短路,建图太麻烦,略过…… #include <cstdio> #include <cstring> #include <queue> const int INF ...

  2. [置顶] c++播放Flash文件

    最近由于需要在程序中使用Flash播放,所以学习了下如何播放Flash,这里使用atl库中的CAxWindow来处理我们要播放的Flash!由于Flash的很多接口我们都不知道,所以可以参考前一篇文章 ...

  3. 淘特房产CMS系统 7.5

    资源描写叙述: 淘特房产CMS系统採用淘特AspCms开发,全部前台信息生成静态HTM,提供了楼盘.二手房.房产中介.房产经济人.业主社区等管理模块,集成了淘特CMS与动网论坛,Discuz,Oblo ...

  4. BZOJ3231(矩阵连乘,稍有点复杂)

    题目:3231: [Sdoi2008]递归数列 题意: 一个由自然数组成的数列按下式定义:   对于i <= k:ai = bi 对于i > k: ai = c1ai-1 + c2ai-2 ...

  5. SQL执行效率总结

    1.关于SQL查询效率,100w数据,查询只要1秒,与您分享: 机器情况 p4: 2.4 内存: 1 G os: windows 2003 数据库: ms sql server 2000 目的: 查询 ...

  6. Node.js入门-Node.js 介绍

    Node.js 是什么 Node.js 不是一种独立的语言,与 PHP,Python 等"既是语言优势平台"不同,它也不是一个 JavaScrip 框架,不同于 CakePHP,D ...

  7. js动画学习(一)

    一.运动框架实现思路 1.匀速运动(属性值匀速变化)(改变 left, right, width, height, opacity 等): 2.缓冲运动(属性值的变化速度与当前值与目标值的差成正比): ...

  8. Jquery Select 下拉框处理

    $("#select").empty();//清空 $("#select").append($("<option/>").val ...

  9. C# 日期格式转换 string类型 20150329 转换为 2015/03/29

    DateTime.ParseExact("20150329", "yyyyMMdd", System.Globalization.CultureInfo.Cur ...

  10. Tableau Server 8.0 升级到 8.3 过程记录

    一.使用账号(管理员权限),安装文件复制到服务器 二.检查维护状态 如果维护状态过期,更新到新版本会变成未授权. 先进Manage Product Keys刷新一下维护日期(其实不刷新也无所谓.到时候 ...