leetcode — decode-ways
/**
* Source : https://oj.leetcode.com/problems/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.
*/
public class DecodeWays {
/**
* 找出有多少种解码方式
*
* 使用递归,但是复杂度较高,可以考虑使用DP
*
* 当XY > 26的时候 dp[i+1] = dp[i]
* XY <= 26 的时候 dp[i+1] = dp[i] + dp[i-1]
*
*
* 临界条件:
* X = 0,dp[i+1] = dp[i]
* Y = 0,dp[i+1] = dp[i-1]
*
* @param digits
*/
public int findWays (String digits) {
if (digits == null || digits.length() == 0 || digits.charAt(0) < '1' || digits.charAt(0) > '9') {
return 0;
}
int[] dp = new int[digits.length() + 1];
dp[0] = dp[1] = 1;
for (int i = 1; i < digits.length(); i++) {
if (digits.charAt(i) > '9' || digits.charAt(i) < '1') {
return 0;
}
int x = digits.charAt(i-1) - '0';
int y = digits.charAt(i) - '0';
int xy = x * 10 + y;
if (xy > 9 && xy <= 26) {
dp[i+1] = dp[i] + dp[i-1];
} else if (y != 0) {
dp[i+1] = dp[i];
}
if (dp[i+1] == 0) {
return 0;
}
}
return dp[dp.length-1];
}
public static void main(String[] args) {
DecodeWays decodeWays = new DecodeWays();
System.out.println(decodeWays.findWays(""));
System.out.println(decodeWays.findWays("1"));
System.out.println(decodeWays.findWays("12"));
System.out.println(decodeWays.findWays("32"));
System.out.println(decodeWays.findWays("10"));
System.out.println(decodeWays.findWays("00"));
System.out.println(decodeWays.findWays("09"));
}
}
leetcode — decode-ways的更多相关文章
- [LeetCode] Decode Ways 解码方法
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- [LeetCode] Decode Ways II 解码方法之二
A message containing letters from A-Z is being encoded to numbers using the following mapping way: ' ...
- LeetCode:Decode Ways 解题报告
Decode WaysA message containing letters from A-Z is being encoded to numbers using the following map ...
- [leetcode]Decode Ways @ Python
原题地址:https://oj.leetcode.com/problems/decode-ways/ 题意: A message containing letters from A-Z is bein ...
- [Leetcode] Decode Ways
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- [LeetCode] Decode Ways(DP)
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- [LeetCode] Decode Ways 解题思路
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- [LeetCode] Decode Ways [33]
题目 A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A ...
- [LeetCode] decode ways 解码方式
A message containing letters fromA-Zis being encoded to numbers using the following mapping: 'A' -&g ...
- [LeetCode] Decode Ways 解码方法个数、动态规划
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
随机推荐
- tensorflow 使用 3 模型学习
模型学习 import tensorflow as tf import numpy as np # 生成 100 个随机的点 x_data = np.random.rand( 100 ) y_data ...
- [BZOJ1925][SDOI2010]地精部落(DP)
题意 传说很久以前,大地上居住着一种神秘的生物:地精. 地精喜欢住在连绵不绝的山脉中.具体地说,一座长度为 N 的山脉 H可分 为从左到右的 N 段,每段有一个独一无二的高度 Hi,其中Hi是1到N ...
- [POJ1273][USACO4.2]Drainage Ditches (网络流最大流)
题意 网络流最大流模板 思路 EK也不会超时 所以说是一个数据比较水的模板题 但是POJ有点坑,多组数据,而且题目没给 哭得我AC率直掉 代码 用的朴素Dinic #include<cstdio ...
- Python(Django)遇到的问题及解决方法
问题一 因为已经有程序占用了Django的默认端口了,所以只要这么启动项目,81是使用的端口,然后访问即可http://127.0.0.1:81/ 解决: 问题二 TypeError: not eno ...
- 锐捷交换机配置DHCP SERVER给固定的MAC地址分配静态IP
今天突发奇想,想给自己的手机分配固定地址,使得接入公司无线网络时每次都取到同一ip地址,这样可以排除认证登录问题. 上网溜达一下,记录下锐捷官方的[常见问题]如下,经验证可行. 需求: 给MAC地址为 ...
- CSS-默认padding 和 margin
一.h1~h6标签:有默认margin(top,bottom且相同)值,没有默认padding值. 在chrome中:16,15,14,16,17,19; 在firefox中:16,15,14,16, ...
- C# 数组结构
数组结构: Array :在内存上是连续分配的,而且元素类型是一致的: 特点:是读取快 可以坐标访问 但是增删慢,长度不能变 比如 int[] intArray=new int[20]; intArr ...
- 微信小程序wx.request请求用POST后台得不到传递数据
微信小程序的wx.request请求,method设为POST并向后台传递数据,但从后台返回的信息来看后台并没有获得传递的数据 wx.request({ url: 'url' ...
- [Swift]LeetCode240. 搜索二维矩阵 II | Search a 2D Matrix II
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- 执行find / -name *.sh时报错 find: 路径必须在表达式之前: start-ressvr-release.sh
想查找一个包含4000多文件的目录下所有.sh结尾的文件 使用命令 find ./ -name *.sh (本身已经在要查找的目录里了) 结果报错: 解决方法一:find ./ - ...