[LeetCode]--131. Palindrome Partitioning(backTracking && DFS && DP)
来源:互联网 发布:大数据分析工程师 编辑:程序博客网 时间:2024/04/28 21:10
1. Problem
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"]]
2. Solution
2.1. BackTracking DFS Thought
利用backTracking()的方法, 不断进行回溯处理。并且进行DFS处理
public class Solution { public List<List<String>> partition(String s) { List<List<String>> res = new ArrayList<List<String>>(); List<String> list = new ArrayList<String>(); dfs(s,0,list,res); return res; } public void dfs(String s, int pos, List<String> list, List<List<String>> res){ if(pos==s.length()) res.add(new ArrayList<String>(list)); else{ for(int i=pos;i<s.length();i++){ if(isPal(s,pos,i)){ list.add(s.substring(pos,i+1)); dfs(s,i+1,list,res); list.remove(list.size()-1); } } } } public boolean isPal(String s, int low, int high){ while(low<high) if(s.charAt(low++)!=s.charAt(high--)) return false; return true; }}
2.2. DP Solution
public class Solution { public static List<List<String>> partition(String s) { int len = s.length(); List<List<String>>[] result = new List[len + 1]; result[0] = new ArrayList<List<String>>(); result[0].add(new ArrayList<String>()); boolean[][] pair = new boolean[len][len]; for (int i = 0; i < s.length(); i++) { result[i + 1] = new ArrayList<List<String>>(); for (int left = 0; left <= i; left++) { if (s.charAt(left) == s.charAt(i) && (i-left <= 1 || pair[left + 1][i - 1])) { pair[left][i] = true; String str = s.substring(left, i + 1); for (List<String> r : result[left]) { List<String> ri = new ArrayList<String>(r); ri.add(str); result[i + 1].add(ri); } } } } return result[len]; }}
Here the pair is to mark a range for the substring is a Pal. if pair[i][j] is true, that means sub string from i to j is pal.
The result[i], is to store from beginng until current index i (Non inclusive), all possible partitions. From the past result we can determine current result.
Reference
- https://discuss.leetcode.com/topic/6186/java-backtracking-solution
- https://discuss.leetcode.com/topic/2884/my-java-dp-only-solution-without-recursion-o-n-2/3
- https://discuss.leetcode.com/category/139/palindrome-partitioning
0 0
- [LeetCode]--131. Palindrome Partitioning(backTracking && DFS && DP)
- leetcode---palindrome-partitioning---dp, dfs
- (M)Backtracking:131. Palindrome Partitioning
- leetcode dfs Palindrome Partitioning
- leetCode解题报告之Palindrome Partitioning I,II(DFS,DP)
- leetCode解题报告之Palindrome Partitioning I,II(DFS,DP)
- leetCode解题报告之Palindrome Partitioning I,II(DFS,DP)
- Palindrome Partitioning I,II[leetcode] DFS以及DP的解法
- leetcode -- Palindrome Partitioning -- 重点 dfs
- LeetCode 131 Palindrome Partitioning (DFS)
- [leetcode][DP] Palindrome Partitioning II
- leetcode---palindrome-partitioning-ii---dp
- LeetCode 131. Palindrome Partitioning
- [LeetCode]131.Palindrome Partitioning
- [Leetcode] 131. Palindrome Partitioning
- [leetcode] 131.Palindrome Partitioning
- [LeetCode]131. Palindrome Partitioning
- 131. Palindrome Partitioning LeetCode
- linux驱动-I2C设备
- 求桥和割点的Tarjan算法
- GM11 C语言实现
- 聊一聊Netty TCP粘包/拆包问题的解决办法
- DSO(dsoframer)的接口文档在VC++使用
- [LeetCode]--131. Palindrome Partitioning(backTracking && DFS && DP)
- Effective Java之对所有对象通用的方法
- Observer模式
- 【Java 并发系列】深入分析Volatile的实现原理
- 后缀表达式
- CSDN博客等级规则
- 在MYSQL中运用全文索引(FULLTEXT index)
- Spring 简介
- unity for android 开发