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.

做了好久才AC,主要是脑子不清楚,思路不清晰。虽然最后想通了但花了一整个下午。

思路:

f(n) 表示,从第n个字符起的字符串可以排列的方式

从后向前判断,如果该数字是0,则只有0种方式;如果数字非0,则该位数字单独解码,方式有f(n - 1)种。如果该数字可以和它后面的数字一起解码,则该数字和他后面的数字一起,方式再加上f(n-2)种。

class Solution {
public:
int numDecodings(string s) {
int iway[] = {,};
int slen = s.length();
if(slen <= )
return ;
iway[] = (s[slen - ] == '') ? : ; for(int i = s.length() - ; i >= ; i--)
{
int curWays = ;
if(s[i] != '')
{
curWays += iway[];
}
int num = (s[i] - '') * + (s[i + ] - '');
if( <= num && num <= )
{
curWays += iway[];
}
iway[] = iway[];
iway[] = curWays;
}
return iway[];
}
};

大神更加简洁的代码,思路差不多,就是从前向后判断的:

int numDecodings(string s) {
// empty string or leading zero means no way
if (!s.size() || s.front() == '') return ; // r1 and r2 store ways of the last and the last of the last
int r1 = , r2 = ; for (int i = ; i < s.size(); i++) {
// zero voids ways of the last because zero cannot be used separately
if (s[i] == '') r1 = ; // possible two-digit letter, so new r1 is sum of both while new r2 is the old r1
if (s[i - ] == '' || s[i - ] == '' && s[i] <= '') {
r1 = r2 + r1;
r2 = r1 - r2;
} // one-digit letter, no new way added
else {
r2 = r1;
}
} return r1;
}

【leetcode】Decode Ways(medium)的更多相关文章

  1. 【leetcode】Decode Ways

    题目如下: 解题思路:这个题目的本质是一个爬楼梯问题,在爬楼梯问题中,每次可以选择走一步或者两步.而本题也是一样的,解码的时候选择解码一个字符或者两个字符,但是加大了一点点难度,要考虑这些情况.1,Z ...

  2. 【leetcode】Single Number (Medium) ☆

    题目: Given an array of integers, every element appears twice except for one. Find that single one. No ...

  3. 【LeetCode】动态规划(下篇共39题)

    [600] Non-negative Integers without Consecutive Ones [629] K Inverse Pairs Array [638] Shopping Offe ...

  4. 【LeetCode】字符串 string(共112题)

    [3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...

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

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

  6. 【LeetCode】 454、四数之和 II

    题目等级:4Sum II(Medium) 题目描述: Given four lists A, B, C, D of integer values, compute how many tuples (i ...

  7. 【LeetCode】18、四数之和

    题目等级:4Sum(Medium) 题目描述: Given an array nums of n integers and an integer target, are there elements ...

  8. 【LeetCode】15、三数之和为0

    题目等级:3Sum(Medium) 题目描述: Given an array nums of n integers, are there elements a, b, c in nums such t ...

  9. 【LeetCode】714、买卖股票的最佳时机含手续费

    Best Time to Buy and Sell Stock with Transaction Fee 题目等级:Medium 题目描述: Your are given an array of in ...

随机推荐

  1. SQLServer安装错误之--->无法打开项 UNKNOWN\Components\DA42BC89BF25F5BD0AF18C3B9B1A1EE8\c1c4f01781cc94c4c8fb1542c0981a2a

    – 错误 1402.无法打开项 UNKNOWN\Components\7ABFE44842C12B390AF18C3B9B1A1EE8\54D21D49F3A8C1C49AC11A1B6445A83E ...

  2. Tomcat 6 --- 你很少使用的安全管理SecurityManager

    试想一下,如果你的JSP页面中包含一句代码“System.exit(1);”,你的web应用访问到该JSP时,会发生什么? 一般使用tomcat可能都没有注意到这个问题,本篇主要讲述tomcat 6中 ...

  3. 【深入JVM内核—原理、诊断与优化】第2期开课了

    [深入JVM内核—原理.诊断与优化]的讲师“葛一鸣”,人称“一哥”,毕业于浙江工业大学,计算机软件与理论专业硕士,是国家认证系统分析师,OCP.2012年出版过<Java程序性能优化>,荣 ...

  4. 关于hibernate链接数据源的配置参数详细解释(转)

    具体使用方法还可以参考以下地址: http://blog.csdn.net/xb12369/article/details/41517409 以下信息转自: http://baike.baidu.co ...

  5. java系列-使用maven创建web项目(二)

    推荐2个maven找jar包配置的网站,只需要搜索关键字即可找到需要的Jar包,非常方便,比如:MySQL就可以找到mysql-connect-Java.jar. http://search.mave ...

  6. Ubuntu 12 安装 MySQL 5.6.26 及 问题汇总

    参考先前的文章:Ubuntu 14 编译安装 PHP 5.4.45 + Nginx 1.4.7 + MySQL 5.6.26 笔记 安装过程: #安装依赖库 sudo apt-get install ...

  7. Triangle leetcode

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  8. .Net的要知道的一些事

    1.什么是.NET?什么是CLI?什么是CLR?IL是什么?JIT是什么,它是如何工作的?GC是什么,简述一下GC的工作方式? .Net是微软推出的框架 CLI是公共语言接口(规范) CLR是公共语言 ...

  9. hdu4747——Mex

    1.题目大意:对一个序列的每一个区间求Mex,最后所有的mex相加(mex就是SG的那个),力求nlogn... 2.分析:最近开始刷线段树了,还是有很多不会啊 首先把1-1 1-2 1-- 1-n这 ...

  10. .net获取周几(中文)

    DateTime.Now.ToString("yyyy年MM月dd日 星期ddd hh时mm分ss秒", new System.Globalization.CultureInfo( ...