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.
题目大意:按照他的映射关系,给定的字符串有多少种解析的方法;
思路 : 这道题,很像求某个集合的所有子集;
比如说,求{1,2,3,4} 的子集,那么sub(4) = sub{3} + (sub{3}里面的每个子集都加4);
而这里,s = "1254" , decode(1) = 1 decode(2) 可以在decode(1)的每种解析方法后+2 , 也可以和1组合 decode(2) = decode(1) + 1 ;
decode(3) , 5 可以放入decode(2)中的任意解析方法,5也可以和组合,然后在放入decode(1)的任意方法中!
所以 ,得到递归式:
decode(n) = decode(n - 1) + decode(n - 2) n与n-1能组合且n != 0 decode(n) = decode(n - 2) n == 0 且 n 与 n-1能组合 decode(n) = decode(n - 1) n != 0 且n , n-1 不能组合 decode(n) = 0 n == 0 且 n , n - 1 不能组合
这道题目,调了好久,细节问题!
public class Solution { public int numDecodings(String s) { int len = s.length(); int[] conseq = new int[len + 1]; if (len == 0) return 0; char ch = s.charAt(0); if (ch == '0') return 0; conseq[0] = 1; conseq[1] = 1; if (len < 2) return 1; for(int i = 2; i <= len ; i++) { // 如果取得当前字符=='0'而且还不能与前面那个元素组合的话,那么整个字符串不能decode if (s.charAt(i - 1) == '0' && !canCombination(s , i - 1) ) { conseq[len] = 0; break; } // 能与前面那个组合 if (canCombination(s , i - 1)){ // 如果他== 0 ,那么它就只能与前一个组合了,解码的方式只有 i- 2有关 if (s.charAt(i - 1) == '0' ) conseq[i] = conseq[i - 2]; else // != 0 ,那么它可以自己组合,也可以和前一个组合 conseq[i] = conseq[i - 1] + conseq[i - 2]; } else { conseq[i] = conseq[i - 1]; } } return conseq[len]; } // 细节地方出错!! public boolean canCombination(String s , int idx) { int pre = Character.getNumericValue(s.charAt(idx - 1) ); int cur = Character.getNumericValue(s.charAt(idx)); if (pre <= 0 || pre > 2) return false; if (pre == 2) { if (cur > 6) return false; return true; } if (cur < 0) return false; return true; } }
相关推荐
java java_leetcode题解之Decode Ways.java
java java_leetcode题解之Decode Ways II.java
"DEcode Ways"是LeetCode中的第91题,它涉及到字符串处理和动态规划的算法知识。下面将详细讨论这个题目以及如何使用JavaScript来解决它。 解题思路: 问题描述:给定一个只包含大写字母的非空字符串s,已知每个...
java java_91-decode-ways
python python_leetcode题解之091_Decode_Ways
javascript js_leetcode题解之91-decode-ways.js
c语言基础 c语言_leetcode题解之0091_decode_ways.zip
例如,著名的“解码方法”问题(Decode Ways)就要求计算给定的数字编码有多少种可能的解码方式,这需要对动态规划有深入的理解。 在解决LeetCode的解码问题时,关键在于理解编码的逻辑,然后选择合适的数据结构和...
Ways](./leetcode/动态规划-Decode Ways.java) [动态规划-Distinct Subsequences](./leetcode/动态规划-Distinct Subsequences.java) [动态规划-Longest Valid Parentheses](./leetcode/动态规划-Longest Valid ...
leetcode 分类 LeetCode Progress 128/154 Other Solutions C++,有详细思路解释 ...Decode Ways Palindrome Partitioning II Maximal Rectangle ###Recursion N-Queens N-Queens II Balanced Binary Tree Binar
加油站 leetcode 【演示记录】 报告 展示 2017/03/06 1.二和,167....2107/03/06 15.3 总和,16.3 ...91.Decode Ways, 96.Unique Binary Search Tree, 120.Triangle, 139.Word Break, 152.Maximum Produ
leetcode怎么计算空间复杂度是指 LeetCode-Solution my first solution of LeetCode 2015-5-7 Problem 95,98(80 ...我经常在递归的结束地方忘记return!...091:Decode Ways 简单的一维DP,用额外数组O(n)即可。 139,1
扩展矩阵leetcode interview-algorithm leetCode 待解决 上楼梯问题 how many ways to decode this message @leetCode 91
There’s basically two ways to measure this: same-bitrate (e.g. a 500kbps VP8 file vs. a 500kbps VP9 file, where the VP9 file likely looks much better), or same-quality (e.g. a VP8 file with SSIM=...
1997 - 2003 Sergio A....and fixed it. Some minor bugs that I don‘t remember fixed.- Added MIME-compliant base64 support (not for use by now). Added examples.0.9.2.1b- Fixed a bug when send a mail and ...
Yes, this possibly is one of the worst ways to do this, //but RAM is at a premium here, and this works for most of the cases. int ICACHE_FLASH_ATTR openConn(const char *streamHost, const char *...