Palindrome Partitioning——LeetCode

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

题目大意:给一个字符串,输出这个字符串所有可能的回文子串。

解题思路:这道题我是用回溯来解,dfs之后再还原状态,首先确定解由两部分构成,每次只拆分后半部分,验证前半部分,如果前面是回文,则递归拆分并验证后半部分。

注意:DFS之后要还原状态,从上一个合法解之后继续遍历其他可能。

Talk is cheap>>

public class PalindromePartitioning {
    public List<List<String>> partition(String s) {
        List<List<String>> res = new ArrayList<>();
        ArrayList<String> tmp = new ArrayList<>();
        int length = s.length();
        dfs(s, tmp, res, length);
        return res;
    }

    public void dfs(String src, ArrayList<String> tmp, List<List<String>> res, int length) {
        if (length == 0) {
            res.add((ArrayList<String>) tmp.clone());
            return;
        }
        for (int i = 1; i <= src.length(); i++) {
            if (isValid(src.substring(0, i))) {
                tmp.add(src.substring(0, i));
                dfs(src.substring(i, src.length()), tmp, res, length - i);
                tmp.remove(tmp.size() - 1);
            }
        }
    }

    public boolean isValid(String s) {
        if (s == null || s.length() <= 1) {
            return true;
        }
        int i = 0, j = s.length() - 1;
        while (i < j) {
            if (s.charAt(i) != s.charAt(j)) {
                return false;
            }
            i++;
            j--;
        }
        return true;
    }
}
时间: 2024-08-28 12:20:19

Palindrome Partitioning——LeetCode的相关文章

Palindrome Partitioning leetcode java

题目: 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",&

Palindrome Partitioning -- leetcode

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

Palindrome Partitioning[leetcode] DFS以及DP的解法

第一种方法是DFS,将所有可能的前缀找到,递归调用partition(剩余字符串) 复杂度为O(2^n) 代码如下: vector<vector<string>> partition(string s) { vector<vector<string>> res; vector<string> patition; if (s.size() == 0) return res; partition(s, patition, res); return r

[LeetCode]132.Palindrome Partitioning II

题目 Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s. For example, given s = "aab", Return 1 since the palindrome partitioning ["aa&qu

[leetcode]Palindrome Partitioning @ Python

原题地址:https://oj.leetcode.com/problems/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 [

[LeetCode] Palindrome Partitioning II 解题笔记

Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s. For example, given s = "aab",Return 1 since the palindrome partitioning ["aa",

leetcode之Palindrome Partitioning

方法一:DFS递归,判断每一个是否为回文数1,首先要有一个判断字符串是否是回文的函数.容易实现,字符串从两边同时往中间走,看字符是否相同; 2,深度优先搜索思想对字符串进行遍历.得到结果.例如,s = "abacd"; 需要对"a""ad""aba""abac""abacd"进行深度优先搜索.深度搜索的过程如下:先选"a",发现"a"是回文,则深度

LeetCode——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","

[LeetCode] Palindrome Partitioning II [12]

题目 Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s. For example, given s = "aab", Return 1 since the palindrome partitioning ["aa&qu