【leetcode】Decode Ways(medium)
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)的更多相关文章
- 【leetcode】Decode Ways
题目如下: 解题思路:这个题目的本质是一个爬楼梯问题,在爬楼梯问题中,每次可以选择走一步或者两步.而本题也是一样的,解码的时候选择解码一个字符或者两个字符,但是加大了一点点难度,要考虑这些情况.1,Z ...
- 【leetcode】Single Number (Medium) ☆
题目: Given an array of integers, every element appears twice except for one. Find that single one. No ...
- 【LeetCode】动态规划(下篇共39题)
[600] Non-negative Integers without Consecutive Ones [629] K Inverse Pairs Array [638] Shopping Offe ...
- 【LeetCode】字符串 string(共112题)
[3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...
- 【LeetCode】91. Decode Ways 解题报告(Python)
[LeetCode]91. Decode Ways 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...
- 【LeetCode】 454、四数之和 II
题目等级:4Sum II(Medium) 题目描述: Given four lists A, B, C, D of integer values, compute how many tuples (i ...
- 【LeetCode】18、四数之和
题目等级:4Sum(Medium) 题目描述: Given an array nums of n integers and an integer target, are there elements ...
- 【LeetCode】15、三数之和为0
题目等级:3Sum(Medium) 题目描述: Given an array nums of n integers, are there elements a, b, c in nums such t ...
- 【LeetCode】714、买卖股票的最佳时机含手续费
Best Time to Buy and Sell Stock with Transaction Fee 题目等级:Medium 题目描述: Your are given an array of in ...
随机推荐
- PHP 线程安全与非线程安全版本的区别深入解析
Windows版的PHP从版本5.2.1开始有Thread Safe(线程安全)和None Thread Safe(NTS,非线程安全)之分,这两者不同在于何处?到底应该用哪种?这里做一个简单的介绍 ...
- linq学习
最全的linq学习文章: http://www.cnblogs.com/heyuquan/p/Linq-to-Objects.html
- [译]通过IIS Request Filtering解决SQL Server CPU高的问题
原文http://www.peterviola.com/solving-sql-server-high-cpu-with-iis-request-filtering/ Top Queries by T ...
- 【翻译】Tomcat 6.0 部署与发布
本篇参考Tomcat官方文档:<First Webapp>翻译,并结合自己的开发经验介绍关于tomcat部署以及发布的相关内容. 1 目录结构 在tomcat中所有的应用都是放置在CATA ...
- IOC和bean容器
- iOS之下拉放大,上推缩小,一个方法搞定
先来看看效果吧. 讲讲大概的实现思路:1、创建头部的视图和tableview,需要注意的是tableview要设置contentInset,contentInsent 的顶部要和头部视图的背景图的高度 ...
- 关于转录组比对STAR软件使用
参考文章:http://weibo.com/p/23041883f77c940102vbkd?sudaref=passport.weibo.com 软件连接:https://github.com/al ...
- updatepanel用法之triggers(局部刷新,全部刷新)使用示例
asyncpostbacktrigger(异步回调触发器):局部刷新,只刷新updatepanel内部的内容postbacktrigger(普通回调触发器):全部刷新 <asp:ScriptMa ...
- vim 编辑器的设置
vi编辑器的配置:http://blog.mcuol.com/User/fenghua/Article/17411_1.htm ******************************vim ~/ ...
- Unity手游之路<十一>资源打包Assetbundle
http://blog.csdn.net/janeky/article/details/17652021 在手游的运营过程中,更新资源是比不可少的.资源管理第一步是资源打包.传统的打包可以将所有物件制 ...