Leetcode: Decode ways
来源:互联网 发布:js移除display属性 编辑:程序博客网 时间:2024/05/08 20:58
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.
采用动态规划来解决,假设知道对于字符串s,从位置0解析到n-1和n-2时的解法数d[n-1]和d[n-2],那对于第n个字符就会有以下几种情况:
1.s[n]为0,且它与s[n-1]的组合是违法的,那s[n] = 0;
2.s[n]为0,但它与s[n-1]是合法组合,那s[n] = s[n-2];
3.s[n]不为0,且它与s[n-1]是合法组合,那s[n] = s[n-1] + s[n-2];
4.s[n]不为0,但它与s[n-1]的组合是违法的,那s[n] = s[n-1]
具体代码实现如下:
<pre name="code" class="cpp">class Solution {public: static int numDecodings(string s) { if (s.empty() || s[0] == '0') return 0; if (s.size() == 1) return 1; //第一个字符肯定是合法的数字,只有一种解法 int preSecond = 1; int preOne = 0; if (canCompose(s[0], s[1]) && s[1] != '0') preOne = 2; else if ((canCompose(s[0], s[1]) && s[1] == '0') //第二个字符为0,只能与第一个字符组合 || (!canCompose(s[0], s[1]) && s[1] != '0') //第二个字符不为0,但不能与第一个字符组合,只能分开解析 ) preOne = 1; else //无论如何前两个字符都无解 return 0; for (int i = 2; i < s.size(); ++i) { if (s[i] == '0') { if (canCompose(s[i - 1], s[i])) //情况1 std::swap(preOne, preSecond); else //情况2 return 0; } else if (canCompose(s[i - 1], s[i])) { //情况3 preOne += preSecond; preSecond = preOne - preSecond; } else //情况4 preSecond = preOne; } return preOne; }private: static bool canCompose(char chPre, char chPost) { if (chPre == 0) return false; if (chPre == '1' || (chPre == '2' && chPost <= '6')) return true; return false; }};
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
- ubuntu14.04 64位兼容32位方法
- java的反射。
- git使用简介
- RPG类型游戏—4
- eclipse安装maven插件
- Leetcode: Decode ways
- 使用AJAX时Illegal invocation错误
- php 表单提交大量数据发生丢失的解决方法
- DoraCMS 安装教程(windows)
- 网页版右下聊天室
- Android Hawk数据库 github开源项目
- leetcode -- Edit Distance -- 重点dp
- GDR(Gradual Decoding Refresh, GradualDecoder Refresh)
- poj1661Help Jimmy【记忆化搜索】