91. Decode Ways
来源:互联网 发布:天谕男玉虚捏脸数据图 编辑:程序博客网 时间:2024/06/16 10:50
91. Decode Ways
题目描述: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.题目大意:给定一个已编码的字符串,找出可以有多少种解码方式
思路:DP,dp[i]表示第i个字符有多少解码方式。
代码
package String;/*** @Author OovEver* @Date 2017/12/11 21:03*/public class LeetCode91 { public static int numDecodings(String s) { if (s == null || s.length() == 0) { return 0; } int n = s.length();// dp表示第i个元素有几种decode方式 int[] dp = new int[n + 1];// 空元素有一种解压方式 dp[0] = 1;// 第一个元素的解压方式 dp[1] = s.charAt(0) == '0' ? 0 : 1; for(int i=2;i<=n;i++) { int first = Integer.parseInt(s.substring(i - 1, i)); int second = Integer.parseInt(s.substring(i - 2, i)); if (first >= 1 && first <= 9) { dp[i] += dp[i - 1]; } if (second >= 10 && second <= 26) { dp[i] += dp[i - 2]; } } return dp[n]; }}
阅读全文
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
- 91. Decode Ways
- Algorithms-91.Decode Ways
- [leetcode] 91. Decode Ways
- 91. Decode Ways
- Leetcode 91. Decode Ways
- 91. Decode Ways
- leetcode 91. Decode Ways
- 91. Decode Ways
- 91. Decode Ways
- Leetcode 91. Decode Ways
- leetcode 91. Decode Ways
- LeetCode 91. Decode Ways
- 91. Decode Ways
- 插入排序
- 标准 IO 库的缓冲区
- HTTP POST GET 本质区别详解
- springboot提示No identifier specified for entity
- 12.11学习心得
- 91. Decode Ways
- 结构体的存储分配问题
- 机器学习笔记(V)线性模型(I)一维最小二乘法
- 关于c++和C的getXXX系列函数
- DataStructureProject(3)
- linux基本操作---用户以及文件权限
- Dagger2 同时引用多个 Module 的三种写法
- Lvs+Ldirectord+Heartbeat
- canvas雨滴绘制总结(三)