其他分享
首页 > 其他分享> > LeetCode 0131 Palindrome Partitioning

LeetCode 0131 Palindrome Partitioning

作者:互联网

原题传送门

1. 题目描述

2. Solution 1

1、思路分析
回溯法。

2、代码实现

package Q0199.Q0131PalindromePartitioning;

import java.util.ArrayList;
import java.util.List;

/*
    Backtracking
 */
public class Solution {

    public List<List<String>> partition(String s) {
        List<List<String>> result = new ArrayList<>();
        ArrayList<String> curRet = new ArrayList<>();
        dfs(s, 0, result, curRet);
        return result;
    }

    private void dfs(String s, int pos, List<List<String>> result, List<String> curRet) {
        if (pos == s.length()) {
            result.add(new ArrayList<>(curRet));
            return;
        }
        for (int i = pos; i < s.length(); i++) {
            if (isPalindrome(s, pos, i)) {
                curRet.add(s.substring(pos, i + 1));
                dfs(s, i + 1, result, curRet);
                curRet.remove(curRet.size() - 1);
            }
        }
    }

    private boolean isPalindrome(String s, int lo, int hi) {
        while (lo < hi)
            if (s.charAt(lo++) != s.charAt(hi--))
                return false;
        return true;
    }
}

3、复杂度分析
时间复杂度:

标签:Partitioning,Palindrome,int,ArrayList,List,pos,curRet,result,LeetCode
来源: https://www.cnblogs.com/junstat/p/16287162.html