Decode Ways -- leetcode
来源:互联网 发布:三维数据图表ae模板 编辑:程序博客网 时间:2024/05/22 12:28
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.
基本思路
和问题 Climbing Stairs 类似
第i位之前,decode共有两种途径:
1. 第i-1位能独立decode
2. 第i-2 和 i-1位,两位一起被当作个整体decode
则deocde方法数,为上面两个途径之和。
此代码在leetcode上,实行执行时间为5ms。
class Solution {public: int numDecodings(string s) { if (s.empty()) return 0; int w2 = 1; int w1 = s[0] != '0' ? 1 : 0; int w = w1; for (int i=2; i<=s.size(); i++) { w = (s[i-1] == '0') ? 0 : w1; if (s[i-2] == '1' || s[i-2] == '2' && s[i-1] >= '0' && s[i-1] <= '6') w += w2; w2 = w1; w1 = w; } return w; }};
0 0
- Leetcode 91. Decode Ways&&639.Decode ways
- LeetCode: Decode Ways
- [LeetCode]Decode Ways
- LeetCode Decode Ways
- LeetCode : Decode Ways
- [Leetcode] Decode Ways *
- [leetcode] Decode Ways (!!)
- leetcode 17: Decode Ways
- Leetcode: Decode ways
- LeetCode Decode Ways
- [LeetCode]Decode Ways
- [leetcode] Decode Ways
- Leetcode: Decode Ways
- [LeetCode] Decode Ways
- [leetcode]Decode Ways
- LeetCode-Decode Ways
- [leetcode] Decode Ways
- [LeetCode] Decode Ways
- AH协议与ESP协议简析
- Android 软键盘的显示与隐藏(isActive一直为true的情况)
- 同步、异步和阻塞、非阻塞
- NIO文章之一个线程同时监听2个socket
- c++学习7--《c++必知必会》小结3
- Decode Ways -- leetcode
- android 判断输入法是否打开
- XenServer中的xentools安装方法
- Shell遍历目录及文件夹中文件
- Windows/Linux环境下收集JVM内存dump文件方法
- BBB IRQ 驱动分析
- CMake 输出所有的编译过程
- LINK1123:failure during conversion to COFF:file invalid or corrupt
- AOSP SDK 报文数据加解密规则