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' - ...
随机推荐
- vimtutor——vim官方教程
=============================================================================== = 欢 迎 阅 ...
- SharePoint2016: 使用powerShell启用project web app
1. 创建pwa承载的webApplication 在SharePoint2016管理中心>应用程序管理>管理web应用程序,新建web应用程序>sharepoint-1001, ...
- C语言可变参数
前段时候在实现利用redis进行的一个数据库比对的功能,稍微去分析了一下redis里面的源代码,然后发现其中的发送命令接口声明如下: void *redisCommand(redisConnect * ...
- Charles抓包软件简介
Charles简介: Charles是一款抓包神器,因为他是基于 java 开发的,所以跨平台,Mac.Linux.Window下都是可以使用的,确保安装之前已经安装了JDK.Charles官网地址: ...
- 源码安装python +NGINX 的坎坷路 +uwsgi安装 部署django 的CRM项目
一.Nginx安装(基于ubuntu17.10 版本) 首先我们是基于源码安装,主要有如下步骤 1.安装依赖包 1.安装gcc g++的依赖库 sudo apt-get install build-e ...
- sql_update
学习: http://www.runoob.com/mysql/mysql-tutorial.html replace: update table_name set column = replace ...
- Android Studio 直播弹幕
我只是搬运:https://blog.csdn.net/HighForehead/article/details/55520199 写的很好很详细,挺有参考价值的 demo直通车:https://do ...
- webpack 解决 semantic ui 中 google fonts 引用的问题
semantic ui css 的第一行引用了 google web font api,由于不可告人而又众所周知的原因,这条链接在国内无法访问: @import url('https://fonts. ...
- JavaScript 异步编程的前世今生(下)
ES6 中的 Generator 在 ES6 出现之前,基本都是各式各样类似Promise的解决方案来处理异步操作的代码逻辑,但是 ES6 的Generator却给异步操作又提供了新的思路,马上就有人 ...
- [Swift]LeetCode165. 比较版本号 | Compare Version Numbers
Compare two version numbers version1 and version2.If version1 > version2 return 1; if version1 &l ...