题目

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. JAVA GUI学习 - JOptionPane对话框组件学习

    /** * 对话框 - 学习笔记 * @author Wfei * */ public class JoptionPaneKnow extends JFrame { public JoptionPan ...

  2. Java开发岗位面试题

    看到一些java面试题,准备慢慢自己做出来试试. 一.Java基础 1. String类为什么是final的. 只有当字符串是不可变的,字符串池才有可能实现.字符串池的实现可以在运行时节约很多heap ...

  3. lua的前景??

    除了专业的游戏公司,貌似很少人在用lua来做开发啊,国内的lua社区越来越不行了. lua还在不断的发展,但每次新版本c接口都改动很大,项目想要升级有点困难啊. lua还有前途吗?

  4. express文件上传

    安装express,创建项目,添加sqlite3模块 express --sessions --css stylus --ejs myhotel npm install sqlite3node app ...

  5. Cocos2d-x 3.0 使用TinyXml 解析XML文件

    在cocos2d-x 3.0中Xml解析已经不用自己找库了,已经为我们集成好了. text.xml <!--?xml version ="1.0" encoding =&qu ...

  6. oracle 10 g 需要启动的2个服务

    开始-> run -> cmd -> services.msc OracleOraDB10g_home1TNSListener OracleServiceORCL

  7. Flex中神奇的快速辅助 Ctrl+1

    Adobe Flash Builder 中的快速辅助功能提供基于上下文的辅助,有助于您快速执行任务.通过快速辅助,可以在适用于当前代码段的操作列表中选择一个操作. 要调用快速辅助,请在编辑器的上下文菜 ...

  8. UIButton 动态改变文本闪烁问题

    当动态改变(比如一秒改变一次)按钮的Title的时候发现按钮每次都要闪烁一下:解决方法如下: self.settleButton.titleLabel.text = title; [self.sett ...

  9. HDU 1045(质因数分解)

    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   Description Tomor ...

  10. Oracle 创建用户并且授权

    以sysdba登陆 创建用户:CREATE USER username IDENTIFIED BY password; 授予(角色)权限:GRANT CONNECT,RESOURCE TO usern ...