131. Palindrome Partitioning

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"]
]

一刷
题解:
用backtracking + dfs求解
dfs中需要起始位置,设置为left, 然后从left到末尾为right的位置,传入子函数判断是否为palindrome。然后以right的下一个作为dfs的子问题的起始位置。

public class Solution {
    private List curList;
    private List> res;
    
    public List> partition(String str) {
        res = new ArrayList<>();
        curList = new ArrayList<>();
        bt(str, 0);
        return res;
    }
    
    private void bt(String str, int left){
        if(curList.size()>0 && left>=str.length()) res.add(new ArrayList<>(curList));
        
        for(int i=left; i

二刷
DFS+backtracking

public class Solution {
    
    private List curList;
    private List> res;
    
    public List> partition(String s) {
        res = new ArrayList<>();
        curList = new ArrayList<>();
        bt(s, 0);
        return res;
    }
    
    private void bt(String str, int left){
        if(curList.size()>0 && left>=str.length()) res.add(new ArrayList<>(curList));
        for(int i=left; i

你可能感兴趣的:(131. Palindrome Partitioning)