Palindrome Partitioning - Leetcode
来源:互联网 发布:诺基亚5320手电筒软件 编辑:程序博客网 时间:2024/06/10 04:47
思路:在每一步,一一往前搜索并验证是否是回文,如果当前不是回文则停止前进,返回上一个状态并记录。每一次都搜索到结尾,因为这里要累计回文子串。回溯法就可以用到,在这里。
Given a string s, partition s such that every substring of the partition is a palindrome.
Return all possible palindrome partitioning of s.
For example, given s = "aab"
,
Return
[ ["aa","b"], ["a","a","b"] ]DP 方法:这里我用哈希表记录了每一个index-结尾的值。
public class Solution { public List<List<String>> partition(String s) { boolean[][] f = new boolean[s.length()][s.length()];for (int i = f.length - 1; i >= 0; i--) {for (int j = i; j < f.length; j++) {if (s.charAt(i) == s.charAt(j)&& (j - i < 2 || f[i + 1][j - 1] == true))f[i][j] = true;}}HashMap<Integer, List<List<String>>> hm = new HashMap<>();for (int i = f.length - 1; i >= 0; i--) {for (int j = i; j < f.length; j++) {if (f[i][j]) {String parlindrome = s.substring(i, j + 1);if (j < f.length - 1) {for (List<String> kks_list : hm.get(j + 1)) {List<String> ls = new ArrayList<>(kks_list);ls.add(0, parlindrome);if (!hm.containsKey(i)) {List<List<String>> kks = new ArrayList<>();kks.add(ls);hm.put(i, kks);} elsehm.get(i).add(ls);}} else {ArrayList<String> as = new ArrayList<String>();as.add(parlindrome);List<List<String>> lls = new ArrayList<>();lls.add(as);if (!hm.containsKey(i))hm.put(i, lls);elsehm.get(i).add(as);}}}}return hm.get(0); }}
深搜DFS:这个思路清楚就清楚了,复杂度2^n
public class Solution { public List<List<String>> partition(String s) { List<List<String>> result = new ArrayList<>(); List<String> path = new ArrayList<>(); DFS(s, result, 0, path); return result; } private void DFS(String s, List<List<String>> result, int start, List<String> path){ if(start == s.length()){ result.add(<span style="background-color: rgb(255, 255, 51);">new ArrayList<String>(path)</span>); return; } for(int i=start; i<s.length(); i++){ if(isPalindrome(s,start,i)){ // cut from i String sub = s.substring(start, i+1); path.add(sub); DFS(s, result, i+1, path); //continue cutting path.remove(path.size()-1); } } } private boolean isPalindrome(String s, int start, int end){ while(s.charAt(start) == s.charAt(end) && start<end) { start++; end--; } return start>=end; }}
0 0
- 【leetcode】Palindrome Partitioning && Palindrome Partitioning II
- [LeetCode] Palindrome Partitioning && Palindrome Partitioning II
- LeetCode Palindrome Partitioning I&&Palindrome Partitioning II
- LeetCode Palindrome Partitioning, Palindrome Partitioning II
- [Leetcode][python]Palindrome Partitioning/Palindrome Partitioning II
- Leetcode:Palindrome Partitioning & Palindrome Partitioning II
- LeetCode: Palindrome Partitioning
- LeetCode : Palindrome Partitioning II
- [leetcode] Palindrome Partitioning II
- leetcode 122: Palindrome Partitioning
- leetcode:Palindrome Partitioning II
- leetcode:Palindrome Partitioning
- 【leetcode】Palindrome Partitioning
- 【leetcode】Palindrome Partitioning II
- leetcode - Palindrome Partitioning II
- Leetcode - Palindrome Partitioning
- LeetCode之Palindrome Partitioning
- [LeetCode]Palindrome Partitioning
- OD逆向时各编程语言入口特征码
- C++日志模块实现
- action segue介绍
- 领导力的塑造——Leo鉴书85
- 中国领导文化
- Palindrome Partitioning - Leetcode
- UVAOJ 127-"Accordian " Patience | java实现
- YT14-HDU-Eddy的彩票
- Palindrome Partitioning II - Leetcode
- js select的语法
- 欢迎使用CSDN-markdown编辑器
- delphi各个版本编译开关值
- sentence structures经典句型
- 一个好用的程序编辑软件Sublime Text 3