题目

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. struts2自己定义类型转换器

    1.1.  struts2自己定义类型转换器 1)        自定类型转换类,继承DefaultTypeConverter类 package com.morris.ticket.conversio ...

  2. CSDN Markdown简明教程3-表格和公式

    0. 文件夹 文件夹 前言 表格 1 表格 2 表格对齐方式 公式 1 行内公式 2 陈列公式displayed formulas 3 MathJax语法 深入 声明 1. 前言 Markdown是一 ...

  3. Razor强类型视图下的文件上传

    域模型Users.cs using System;using System.Collections.Generic;using System.Linq;using System.Web; namesp ...

  4. js数组练习

    //查找数组对象中 age 大于 18 对象 function filterAdult(arr) { return arr.filter(function(item, index, array) { ...

  5. vagrant 入门2

    创建第一个Vagrant虚拟环境以及工程: (1)创建工程目录, 并且执行vagrant init命令,该命令会产生最初的 Vagrantfile $ mkdir vagrant_guide $ cd ...

  6. poj 2515 Birthday Cake

    /** 大意 : 求1^m + 2^m + 3^m + 4^m +....+ n^m 解题步骤: 先构造从0到m的第1阶差分序列,然后以下所有2---->p阶的差分表. 令C[n+1][1]=n ...

  7. Ajax学习(三)——XMLHttpRequest对象的五步使使用方法

        Ajax的核心技术是XMLHttpRequest对象,它能够在不向server提交整个页面的情况下.实现局部更新网页.通过这个对象,Ajax能够像桌面应用程序那样仅仅与server进行数据层的 ...

  8. error: property&#39;s synthesized getter follows Cocoa naming convention for returning &#39;owned&#39; objects

    出现这样的情况,主要是属性名中包括  keyword. You can solve this by: Renaming that property: @property (strong, nonato ...

  9. 一个例子:HelloWorld

    作为C语言来说,我是用的是QT Creator作为开发工具. 事实上使用什么工具无所谓.重要的是学到实用的知识. 第一个实例程序就是HelloWorld程序.上代码: 版权声明:您好,转载请离开我的博 ...

  10. 初步swift语言学习笔记2(可选类型?和隐式可选类型!)

    作者:fengsh998 原文地址:http://blog.csdn.net/fengsh998/article/details/28904115 转载请注明出处 假设认为文章对你有所帮助.请通过留言 ...