###### Hard-题目40：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”,”b”] could be produced using 1 cut.

（上述两步分开做会使得代价翻倍，容易TLE）

1、min[i]初始化为min[i+1]+1，即初始化s[i]与s[i+1]之间需要切一刀。这里考虑边界问题，因此min数组设为n+1长度。
2、从i到n-1中间如果存在位置j，同时满足：(1)s[i,…,j]为回文串;(2)1+min[j+1] < min[i]。

class Solution {
public:
int minCut(string s) {
int n = s.size();
vector<vector<bool> > isPalin(n, vector<bool>(n, false));
vector<int> min(n+1, -1); //min cut from end

for(int i = 0; i < n; i ++)
{
isPalin[i][i] = true;
}

for(int i = n-1; i >= 0; i --)
{
min[i] = min[i+1] + 1;
for(int j = i+1; j < n; j ++)
{
if(s[i] == s[j])
{
if(j == i+1 || isPalin[i+1][j-1] == true)
{
isPalin[i][j] = true;
if(j == n-1)
min[i] = 0;
else if(min[i] > min[j+1]+1)
min[i] = min[j+1] + 1;
}
}
}
}

return min[0];
}
};

80ms,45.15%,12ms,16.23%

#### 【LeetCode】132. Palindrome Partitioning II 基于动态规划DP、C++、Java的分析及解法

2016-05-16 20:29:43

#### LeetCode 132. Palindrome Partitioning II（回文切分）

2016-05-27 00:22:56

#### Leetcode(132) Palindrome Partitioning II

2014-12-17 03:47:14

#### LeetCode 132 Palindrome Partitioning II--In C++

2016-06-18 11:15:51

#### LeetCode 132 Palindrome Partitioning II (动态规划)

2016-10-23 11:59:51

#### leetcode之 Palindrome Partitioning I&II

2013-11-20 21:06:00

#### 132. Palindrome Partitioning II (动态规划)

2017-05-25 19:09:25

#### Leetocde: Palindrome Partitioning II

2013-10-28 23:41:31

#### 132. Palindrome Partitioning II

2015-08-15 19:52:25

#### LeetCode 之 Palindrome Partitioning II（动态规划）

2015-08-03 10:58:41

## 不良信息举报

Hard-题目40：132. Palindrome Partitioning II