leet-code/src/main/java/leetcode/editor/cn/DecodeWays.java
2021-04-29 23:21:52 +08:00

156 lines
4.6 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//一条包含字母 A-Z 的消息通过以下映射进行了 编码
//
//
//'A' -> 1
//'B' -> 2
//...
//'Z' -> 26
//
//
// 要 解码 已编码的消息,所有数字必须基于上述映射的方法,反向映射回字母(可能有多种方法)。例如,"11106" 可以映射为:
//
//
// "AAJF" ,将消息分组为 (1 1 10 6)
// "KJF" ,将消息分组为 (11 10 6)
//
//
// 注意,消息不能分组为 (1 11 06) ,因为 "06" 不能映射为 "F" ,这是由于 "6" 和 "06" 在映射中并不等价。
//
// 给你一个只含数字的 非空 字符串 s ,请计算并返回 解码 方法的 总数 。
//
// 题目数据保证答案肯定是一个 32 位 的整数。
//
//
//
// 示例 1
//
//
//输入s = "12"
//输出2
//解释:它可以解码为 "AB"1 2或者 "L"12
//
//
// 示例 2
//
//
//输入s = "226"
//输出3
//解释:它可以解码为 "BZ" (2 26), "VF" (22 6), 或者 "BBF" (2 2 6) 。
//
//
// 示例 3
//
//
//输入s = "0"
//输出0
//解释:没有字符映射到以 0 开头的数字。
//含有 0 的有效映射是 'J' -> "10" 和 'T'-> "20" 。
//由于没有字符,因此没有有效的方法对此进行解码,因为所有数字都需要映射。
//
//
// 示例 4
//
//
//输入s = "06"
//输出0
//解释:"06" 不能映射到 "F" ,因为字符串含有前导 0"6" 和 "06" 在映射中并不等价)。
//
//
//
// 提示:
//
//
// 1 <= s.length <= 100
// s 只包含数字,并且可能包含前导零。
//
// Related Topics 字符串 动态规划
// 👍 696 👎 0
package leetcode.editor.cn;
//91:解码方法
public class DecodeWays {
public static void main(String[] args) {
//测试代码
Solution solution = new DecodeWays().new Solution();
//2
System.out.println(solution.numDecodings("12"));
//3
System.out.println(solution.numDecodings("226"));
//0
System.out.println(solution.numDecodings("0"));
//0
System.out.println(solution.numDecodings("06"));
//1
System.out.println(solution.numDecodings("10"));
//1
System.out.println(solution.numDecodings("2101"));
//2
System.out.println(solution.numDecodings("2011"));
//3
System.out.println(solution.numDecodings("1201234"));
//0
System.out.println(solution.numDecodings("10011"));
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int numDecodings(String s) {
// int length = s.length();
// if (is) {
// for (int i = 0; i < length - 1; i++) {
// if (s.charAt(i) == '0' && s.charAt(i + 1) == '0') {
// return 0;
// }
// }
// is = false;
// }
// if (s.startsWith("0")) {
// return 0;
// }
// if (length == 1) {
// return 1;
// }
// if (length == 2) {
// if ((s.charAt(0) - '0') * 10 + (s.charAt(1) - '0') <= 26 && s.charAt(1) != '0') {
// return 2;
// } else {
// return 1;
// }
// }
// if (s.charAt(length - 1) == '0') {
// return numDecodings(s.substring(0, length - 2));
// }
// if (s.charAt(length - 2) == '0') {
// return length > 3 ? numDecodings(s.substring(0, length - 3)) : 1;
// }
// if ((s.charAt(length - 2) - '0') * 10 + (s.charAt(length - 1) - '0') <= 26) {
// return numDecodings(s.substring(0, length - 2)) + numDecodings(s.substring(0, length - 1));
// } else {
// return numDecodings(s.substring(0, length - 1));
// }
if (s.startsWith("0")) {
return 0;
}
int length = s.length();
int[] nums = new int[length + 1];
nums[0] = 1;
for (int i = 1; i <= length; i++) {
if (i < length && s.charAt(i) == '0' && s.charAt(i - 1) == '0') {
return 0;
}
if (s.charAt(i - 1) != '0') {
nums[i] += nums[i - 1];
}
if (i > 1 && s.charAt(i - 2) != '0' && ((s.charAt(i - 2) - '0') * 10 + (s.charAt(i - 1) - '0') <= 26)) {
nums[i] += nums[i - 2];
}
}
return nums[length];
}
}
//leetcode submit region end(Prohibit modification and deletion)
}