LeetCode *** 91. Decode Ways
来源:互联网 发布:嵌入式单片机工程师 编辑:程序博客网 时间:2024/05/08 09:51
题目:
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.
分析:
代码:
class Solution {public:int numDecodings(string s) {int len = s.length(), ppre = 1, pre = 1, res = 0;if (len == 0 || s[0] == '0')return 0;if (len == 1)return 1;for (int i = 1; i<len; ++i) {if (isValid(s[i]) && isValid(s[i - 1], s[i]))res = ppre + pre;if (isValid(s[i]) && !isValid(s[i - 1], s[i]))res = pre;if (!isValid(s[i]) && isValid(s[i - 1], s[i]))res = ppre;if (!isValid(s[i]) && !isValid(s[i - 1], s[i]))return 0;ppre = pre;pre = res;res = 0;}return pre;}bool isValid(char a, char b) {return a == '1' || (a == '2'&&b <= '6');}bool isValid(char c) {return c != '0';}};
0 0
- Leetcode 91. Decode Ways&&639.Decode ways
- [Leetcode] 91. Decode Ways
- LeetCode --- 91. Decode Ways
- [LeetCode]91.Decode Ways
- [leetcode] 91.Decode Ways
- [leetcode] 91. Decode Ways
- Leetcode 91. Decode Ways
- leetcode 91. Decode Ways
- Leetcode 91. Decode Ways
- leetcode 91. Decode Ways
- LeetCode 91. Decode Ways
- LeetCode *** 91. Decode Ways
- Leetcode:91. Decode Ways
- leetcode 91. Decode Ways
- LeetCode-91.Decode Ways
- LeetCode 91. Decode Ways
- leetcode.91. Decode Ways
- [leetcode]91. Decode Ways
- response.setContentType()的作用及参数
- Mysql错误: Lock wait timeout exceeded解决办法
- ScrollView抖动问题的解决
- 分享个人项目中的Log系统: CCLogSystem
- Eclipse中.setting目录下文件介绍
- LeetCode *** 91. Decode Ways
- DWR3.0 如何应用的简单介绍(有实例)
- Mysql学习笔记(三)
- 欢迎使用CSDN-markdown编辑器
- 刨根问底Objective-C Runtime(1)- Self & Super
- LeetCode *** 307. Range Sum Query - Mutable (Binary Indexed Trees)
- 详述Canvas(五)/绘制圆角矩形
- HTTP协议 (3)了解响应报文
- linux 操作