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. spark在windows下的安装

      Windows下最简的开发环境搭建这里的spark开发环境, 不是为apache spark开源项目贡献代码, 而是指基于spark的大数据项目开发. Spark提供了2个交互式shell, 一个 ...

  2. JCarouselLite--帮助文档

    jcarousellite是一款jquery插件,可以控制文档元素滚动,丰富的参数设置可以控制滚动的更多细节,是一款不可多得的滚动插件. ------------------ 官网地址:http:// ...

  3. Windows疑难杂症之开机无法显示桌面。

    开机无法显示桌面可能有以下两种情况. 1.系统故障或病毒引起explorer.exe无法加载启动. 2.注册表故障造成默认的值不是explorer.exe.(可能是安装了某些软件造成此问题) 3,某开 ...

  4. QS2016年全球高等教育系统实力排名 中国排名世界第八亚洲第一

    2016年5月18日,QS发布"2016年全球高等教育系统实力排名",中国在此榜单表现优异,排名世界第八亚洲第一. 排名指标 排名指标及计算方法如下: 系统实力:QS大学排名前70 ...

  5. Java并发包源码学习之AQS框架(三)LockSupport和interrupt

    接着上一篇文章今天我们来介绍下LockSupport和Java中线程的中断(interrupt). 其实除了LockSupport,Java之初就有Object对象的wait和notify方法可以实现 ...

  6. POJ 1947 Rebuilding Roads

    树形DP..... Rebuilding Roads Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 8188 Accepted: ...

  7. git之常用指令

    参考:Git教程 - 廖雪峰的官方网站 1.git  //linux上检测是否安装git 2.sudo apt-get install git //linux上安装git 3.git config - ...

  8. hiho #1310 : 岛屿 (dfs,hash)

    题目2 : 岛屿 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给你一张某一海域卫星照片,你需要统计: 1. 照片中海岛的数目 2. 照片中面积不同的海岛数目 3. 照 ...

  9. OpenCv皮肤检测-HSV分离

    HSV皮肤检测 // 进行肤色检测 void SkinDetect(IplImage* src, IplImage* dst) { // 创建图像头 IplImage* hsv = cvCreateI ...

  10. HDU 1160 DP最长子序列

    G - FatMouse's Speed Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64 ...