Leetcode no. 91
2016-06-22 11:17
155 查看
91. Decode Ways
A message containing letters from
Given an encoded message containing digits, determine the total number of ways to decode it.
For example,
Given encoded message
2) or
The number of ways decoding
public class Solution {
public int numDecodings(String s) {
if (s.length() ==0 || s.charAt(0)== '0') return 0;
int prev= 1, pprev= 1;
for (int i = 1; i < s.length(); i++) {
if (s.charAt(i)=='0') prev=0;
if (s.charAt(i-1)=='1' || (s.charAt(i-1)=='2' && s.charAt(i)-'0'<=6)) {
int tmp = prev;
prev+= pprev;
pprev= tmp;
} else pprev= prev;
}
return prev;
}
}
A message containing letters from
A-Zis 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.
public class Solution {
public int numDecodings(String s) {
if (s.length() ==0 || s.charAt(0)== '0') return 0;
int prev= 1, pprev= 1;
for (int i = 1; i < s.length(); i++) {
if (s.charAt(i)=='0') prev=0;
if (s.charAt(i-1)=='1' || (s.charAt(i-1)=='2' && s.charAt(i)-'0'<=6)) {
int tmp = prev;
prev+= pprev;
pprev= tmp;
} else pprev= prev;
}
return prev;
}
}
相关文章推荐
- C++一些基础
- 快速N次方
- java transient关键字
- win to go在移动硬盘上制作windows系统
- 详解Java泛型type体系整理
- 在属性上面添加这个注释 在unity界面能看到注释内容
- UML关系
- 在linux摸索的过程
- 支付宝
- ubuntu关闭防火墙
- Remove Duplicates from Sorted Array I、II解题报告
- 《CIO新思维III-变革时代的企业IT战略与实务》即将出版,战略观点征集活动中
- Appium移动自动化框架初探
- nginx配置详解
- ubuntu中sudo不能使用的问题,/etc/sudoers文件不存在
- 查看oracle数据库的连接数以及用户
- LeetCode Factorial Trailing Zeroes(计算阶乘结果尾部有几个0)
- 两种方法对activiti运行过的节点高亮显示
- TortoiseSVN--wc.db
- 【AE二次开发】实现鹰眼的联动