给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串。
    返回 s 所有可能的分割方案。
    示例:
    输入: “aab”
    输出:
    [
    [“aa”,”b”],
    [“a”,”a”,”b”]
    ]
    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/palindrome-partitioning


    思路:
    DFS的回溯算法,如果字符串的分段是回文的就截取片段判断后面的
    如果到最后字符串已经被分解的只剩下0的长度,说明就是我们想要的部分,保存到res中

    1. class Solution {
    2. public List<List<String>> partition(String s){
    3. List<List<String>> res = new LinkedList<>();
    4. List<String> path = new ArrayList<>();
    5. dfs(path,res,s);
    6. return res;
    7. }
    8. private void dfs(List<String> path, List<List<String>> res, String s) {
    9. if (s.length() == 0){
    10. res.add(new ArrayList<>(path));
    11. return;
    12. }
    13. for (int i = 0; i < s.length(); i++) {
    14. if (isPalindrome(s,0,i)){
    15. path.add(s.substring(0,i + 1));
    16. dfs(path,res,s.substring(i + 1));
    17. path.remove(path.size() - 1);
    18. }
    19. }
    20. }
    21. //判断是否回文
    22. public boolean isPalindrome(String s,int begin,int end){
    23. while (begin < end){
    24. if (s.charAt(begin) == s.charAt(end)){
    25. begin++;
    26. end--;
    27. }else {
    28. return false;
    29. }
    30. }
    31. return true;
    32. }
    33. }