【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 partitioning ["aa","b"] could be produced using 1 cut.

这题肯定是用动态规划来做的。

——————>

当然递归也能做,每一次划分为两个子串,得到与原问题完全一致的子问题。

终止条件为得到回文串。所求的最小切分数即最大递归深度。

TLE是意料之中的。

<——————

显而易见的是需要开辟二维数组isPar,用于存储已经确定的回文子串。isPar[i][j]==true代表s[i,...,j]是回文串。

然后还需要一个递推过程,从前往后还是从后往前是对称的,以从后往前为例。

开辟一维数组mCut,mCut[i]意为s[i,...,n-1]的最小切分数。

在此有几个关键点说明一下,详细可以看代码和注释:

1、mCut[i]初始化为mCut[i+1]+1,即初始化s[i]与s[i+1]之间需要切一刀。注意边界。

2、从i到n-1中间如果存在位置j,同时满足:(1)s[i,...,j]为回文串;(2)1+mCut[j+1] < mCut[i]。

那么mCut[i]=1+mCut[j+1],也就是说一刀切在j的后面比切在i的后面要好。

这个过程很像Dijkstra算法:d(u,v)+d(v,w)<d(u,w) --> d(u,w)=d(u,v)+d(v,w)

class Solution
{
public:

    int minCut(string s)
    {
        int n = s.size();

        //n*n的二维数组,isPar[i][j]代表s[i,...,j]是否为回文字符串
        //初始化方式为,isPar由n个vector<bool>元素构成,每个vector<bool>由n个false构成
        vector<vector<bool> > isPar(n, vector<bool>(n,false));
        for(int i = 0; i < n; i ++)
            isPar[i][i] = true;

        //mCut[i]代表s[i,...,n-1]的minCut数
        vector<int> mCut(n);            

        for(int i = n-1; i >= 0; i --)
        {
            //mCut[i]初始化为后一位的切分数加1
            mCut[i] = (i==n-1) ? 0:mCut[i+1]+1;

            for(int j = i+1; j < n; j ++)
            {
                if(s[i] == s[j])
                {
                    //更新isPar[i][j]与mCut[i]
                    if(j == i+1 || isPar[i+1][j-1] == true)
                    {
                        isPar[i][j] = true;
                        if(j == n-1)
                            mCut[i] = 0;
                        //[i,..,j]一次cut,[j+1,...,n-1]有mCut[j+1]次cut
                        else if(1+mCut[j+1] < mCut[i])
                            mCut[i] = 1+mCut[j+1];
                    }
                }
            }
        }

        return mCut[0];
    }
};

时间: 2024-10-24 19:17:37

【LeetCode】Palindrome Partitioning II的相关文章

【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

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&qu

【Leetcode】Path Sum II

Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example: Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ / 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] 思路:与[Leetcode]Path Sum 不同

【LeetCode】Spiral Matrix II

Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example,Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ]此题与Spiral Matrix类似,可以用相同的方法解决,相比之下,此题比前一题简单 publ

【leetcode】Word Break II

Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences. For example, givens = "catsanddog",dict = ["cat", "cats"

【Leetcode】Unique Paths II

Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. For example, There is one obstacle in the middl

【LeetCode】Jump Game II

Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum number of jumps

[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