LeetCode: Palindrome Partitioning [131]

【称号】

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

【题意】

给定一个字符串s, 要求对s进行划分,划分后每一个子串都是回文串。

要求返回全部的划分情况

【思路】

直观思路是使用逐层递归。先确定第一个点。然后确定第二个点,再确定第三个点,依次类推。这样的方式的时间复杂度很高。

本题使用DP:先计算出随意两个位置i,j之间的子串是否是回文串,用IsPalindrome[i][j]表示。

【代码】

class Solution {
public:

    void getPartition(vector<vector<string> >&result, vector<string>&splits, int start, string&s, vector<vector<bool> >&isPal){
        //spits-已切分的结果,start-当前切分開始的位置
        if(start==s.length()){
            vector<string> newSplits = splits;
            result.push_back(newSplits);
            return;
        }

        for(int end=start; end<s.length(); end++){
            if(isPal[start][end]){
                splits.push_back(s.substr(start, end-start+1));
                getPartition(result, splits, end+1, s, isPal);
                splits.pop_back();
            }
        }
    }

    vector<vector<string>> partition(string s) {
        vector<vector<string> >result;
        int len=s.length();
        if(len==0)return result;

        vector<vector<bool> > isPal(len, vector<bool>(len, false));
        //初始化isPal[i][i]=true;
        for(int i=0; i<len; i++)
            isPal[i][i]=true;
        //初始化相邻两位字符构成的子串
        for(int i=0; i<len-1; i++)
            if(s[i]==s[i+1])isPal[i][i+1]=true;
        //推断其它i<j的位置
        for(int i=len-3; i>=0; i--)
            for(int j=i+2; j<len; j++)
                isPal[i][j]=(s[i]==s[j]) && isPal[i+1][j-1];

        //确定全部的组合
        vector<string> splits;
        getPartition(result, splits, 0, s, isPal);
        return result;
    }
};

版权声明:本文博主原创文章。博客,未经同意不得转载。

时间: 2024-10-13 20:08:14

LeetCode: Palindrome Partitioning [131]的相关文章

[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

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

LeetCode: Palindrome Partitioning II [132]

[题目] 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 II (DP)

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 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 解题报告

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

指责别人,看清自己 [问题描述] 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

LeetCode: Palindrome Partitioning II 解题报告

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 pa