leetcode || 132、Palindrome Partitioning II

problem:

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.

Hide Tags

Dynamic Programming

题意:求将一个字符串划分为各个子字符串都为回文字符串的最小划分数

thinking:

(1)最优解,DP问题

(2)定义函数

D[i,n] = 区间[i,n]之间最小的cut数,n为字符串长度

a   b   a   b   b   b   a   b   b   a   b   a

i                                n

如果现在求[i,n]之间的最优解?应该是多少?简单看一看,至少有下面一个解

a   b   a   b   b   b   a   b   b   a   b   a

i                  j   j+1 
    n

此时  D[i,n] = min(D[i, j] + D[j+1,n])  i<=j <n。这是个二维的函数,实际写代码时维护比较麻烦。所以要转换成一维DP。如果每次,从i往右扫描,每找到一个回文就算一次DP的话,就可以转换为

D[i] = 区间[i,n]之间最小的cut数,n为字符串长度, 则,

D[i] = min(1+D[j+1] )    i<=j <n

有个转移函数之后,一个问题出现了,就是如何判断[i,j]是否是回文?每次都从i到j比较一遍?太浪费了,这里也是一个DP问题。

定义函数

P[i][j] = true if [i,j]为回文

那么

P[i][j] = ((str[i] == str[j]) && (P[i+1][j-1]));

code:

class Solution
{
public:
int minCut(string s) {
        int len = s.size();
		int* dp = new int[len+1];
		for(int i=len; i>=0; i--)
			dp[i] = len-i;
		bool** matrix = new bool*[len];
		for(int i=0; i<len; i++)
		{
			matrix[i] = new bool[len];
			memset(matrix[i], false, sizeof(bool)*len);
		}
		for(int i=len-1; i>=0; i--)
			for(int j=i; j<len; j++)
			{
				if(s[i] == s[j] && (j-i<2 || matrix[i+1][j-1]))
				{
					matrix[i][j] = true;
					dp[i] = min(dp[i], dp[j+1]+1);
				}
			}
		return dp[0]-1;
    }
};
时间: 2024-08-20 03:22:06

leetcode || 132、Palindrome Partitioning II的相关文章

LeetCode 笔记24 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 ||131、Palindrome Partitioning

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

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

[email&#160;protected] [131/132] Palindrome Partitioning &amp; Palindrome Partitioning II

https://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 [ ["aa&qu

【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 p

132 Palindrome Partitioning II

132 Palindrome Partitioning II 这道题就是标识出s[i:j+1]是否为palindrome, 然后dp找出最小分割 class Solution: # @param {string} s # @return {integer} def minCut(self, s): LS = len(s) dp = [[False] * LS for i in range(0, LS)] ans = [1<<31 for i in range(0 ,LS)] for ls in

[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 和Palindrome Partitioning II

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