LeetCode 91. Decode Ways(解码方法)
2016-05-26 00:47
393 查看
原题网址:https://leetcode.com/problems/decode-ways/
A message containing letters from
mapping:
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
方法:动态规划。
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 == null || s.length() == 0) return 0; char[] sa = s.toCharArray(); int[] nums = new int[sa.length+1]; nums[0] = 1; for(int i=1; i<=sa.length; i++) { if (sa[i-1] != '0') nums[i] += nums[i-1]; if (i>1 && sa[i-2] == '1') nums[i] += nums[i-2]; if (i>1 && sa[i-2] == '2' && sa[i-1] >= '0' && sa[i-1] <= '6') nums[i] += nums[i-2]; } return nums[sa.length]; } }
相关文章推荐
- LeetCode 158. Read N Characters Given Read4 II - Call multiple times(读文件流)
- POJ 2823 Sliding Window
- Android的API版本和名称对应关系
- php urlencode()与urldecode()函数字符编码原理-笔记
- uva10098 全排列分分钟搞定
- 【Android进阶】Android常见的几种RuntimeException
- 数字签名是什么? 对称加密 与非对称加密
- Android Drawable - Clip Drawable使用详解(附图)
- 模拟new实例化对象。
- LeetCode 043 Multiply Strings
- CentOS安装 /BOOT分盘挂载无法修改大小解决方法
- mysql5.1安装与卸载
- android release版本需要数字签名
- Bloom filter(布隆过滤器)概念与原理
- 在VS2010中无法以“(LocalDB)\8.0”为服务器名来创建数据库
- 基于Apache的Tomcat负载均衡和集群
- Linux下将多个静态库(.a)合并成一个静态库文件(.a)的命令操作,方法一
- ctags的简单配置及其使用
- 文章标题
- QML 的 font 与 Canvas 的中 font