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. 解析posix与perl标准的正则表达式区别 ---PHP

        正则表达式(Regular Expression,缩写为regexp,regex或regxp),又称正规表达式.正规表示式或常规表达式或正规化表示法或正规表示法,是指一个用 来描述或者匹配一系 ...

  2. PHP基础 数组函数 的总结

    <?php /** * PHP基础 数组操作函数 * * 指针函数:[类似于数据库的游标] 见例1.1 * current($arr)/pos 返回当前指针指向的元素 * key($arr) 返 ...

  3. oracle查询单表占用空间的大小

    oracle查询单表占用空间的大小 SELECT segment_name AS TABLENAME, BYTES B, BYTES KB, BYTES MB FROM user_segments w ...

  4. 【PHP面向对象(OOP)编程入门教程】6.如何去使用对象中的成员

    上面看到PHP对象中的成员有两种一种是成员属性, 一种是成员方法.对象我们以经可以声明了,$p1=new Person(); 怎么去使用对象的成员呢?要想访问对象中的成员就要使用一个特殊的操作符”-& ...

  5. git之常用指令

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

  6. 《Lua程序设计 第二版》学习笔记一

    Lua简介 Lua是一种简单.可拓展.可移植及高效的脚本语言. 开始 Lua之间不需要分隔符 运行方式: Linux下: lua -i prog dofile("lib1.lua" ...

  7. JAVA操作ORACLE数据库的存储过程

    一.任务提出 JAVA操作oracle11g存储过程实验需要完成以下几个实例: 1.调用没有返回参数的过程(插入记录.更新记录) 2.有返回参数的过程 3.返回列表的过程 4.返回带分页的列表的过程. ...

  8. JQuery选择器细节-遁地龙卷风

    1.层次选择器-子元素选择器 <body> <div> <p>lol</p> <div> <p></p> </ ...

  9. Unity手游之路<二>Java版服务端使用protostuff简化protobuf开发

    http://blog.csdn.net/janeky/article/details/17151465 开发一款网络游戏,首先要考虑的是客户端服务端之间用何种编码格式进行通信.之前我们介绍了Unit ...

  10. [codevs1155][KOJ0558][COJ0178][NOIP2006]金明的预算方案

    [codevs1155][KOJ0558][COJ0178][NOIP2006]金明的预算方案 试题描述 金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间金明自己专用的很宽敞的房间.更让他高兴 ...