[Leetcode] Decode Ways
来源:互联网 发布:ubuntu没有 ssh目录 编辑:程序博客网 时间:2024/05/21 11:36
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.
public class Solution { public int numDecodings(String s) { if(s.length() == 0){ return 0; } int[] result = new int[s.length() + 1]; result[s.length()] = 1; for(int i = s.length() - 1; i >= 0; i--) { if(s.charAt(i) == '0') { result[i] = 0; continue; } result[i] = result[i + 1]; if(canTakeTwoChar(s, i)) { result[i] += result[i+2]; } } return result[0]; } private boolean canTakeTwoChar(String s, int position) { if(s.charAt(position) == '0') { return false; } else if(position >= s.length() - 1){ return false; } int value = (s.charAt(position) - '0') * 10 + (s.charAt(position + 1) - '0'); return value <= 26; }}
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
- GNU tools 使用积累
- 【codevs1217】【noip2012提高day2T2】借教室——线段树做法
- 如何成为一个很厉害的人 - 笔记
- linux程序设计:linux物理内存和虚拟内存
- 将nginx添加为系统服务
- [Leetcode] Decode Ways
- HTML+CSS基础 img标签title属性 鼠标移动到图片上时显示文字
- Opencv 使用Stitcher类图像拼接生成全景图像
- 深入理解Java:注解(Annotation)注解处理器
- Material Design入门(上)
- noi2001 食物链(并查集)
- servlet框架基础
- Codeforces Round #373 (Div. 2) A. Vitya in the Countryside
- BC#87