leetcode 91. Decode Ways
2016-04-05 16:40
429 查看
Question
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.
Code
public int numDecodings(String s) { if (s == null || s.length() == 0) { return 0; } int[] nums = new int[s.length() + 1]; nums[0] = 1; nums[1] = s.charAt(0) != '0' ? 1 : 0; for (int i = 2; i <= s.length(); i++) { if (s.charAt(i - 1) != '0') { nums[i] += nums[i - 1]; } int twoDigits = (s.charAt(i - 2) - '0') * 10 + s.charAt(i - 1) - '0'; if (twoDigits >= 10 && twoDigits <= 26) { nums[i] += nums[i - 2]; } } return nums[s.length()]; }
相关文章推荐
- arm-linux内存管理学习笔记(1)-内存页表的硬件原理
- 使用SurfaceView播放视频
- 通过get或post请求URL获取返回数据
- #pragma和#ifndef的区别
- java多线程之守护线程(Daemon)
- mysql 之mvcc多版本控制
- LOOPS(HDU3853)
- 第一天学习,错误汇总
- Spring MVC 全局异常处理&文件上传
- 数据结构实现循环队列的两种方法
- 一个popen异步的例子
- idea tomcat 热部署
- 游戏开发策划工作(6)
- 阿里云主机windows系统Apache启用浏览器缓存的方法
- Linux内核分析第三章读书笔记
- django之创建第8-3个项目-数据库数据提取之高级操作
- the usage of vi on mac
- PS制作超酷炫的碎裂火焰美女
- c++封装之对象成员基础
- 干活 spring依赖注入