Leetcode 132. Palindrome Partitioning II

https://leetcode.com/problems/palindrome-partitioning-ii/

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
public:
int minCut(string s) {
int n = s.size();
if(!n){
return 0;
}
vector<vector<bool>> judge(n, vector<bool>(n, false));
vector<int> dp(n);

for(int i = 0; i < n; i++){
dp[i] = i;
for(int j = 0; j <= i; j++){
if(s[j] == s[i] && (i - j <= 2 || judge[j+1][i-1])){
judge[j][i] = true;
dp[i] = (j == 0) ? 0 : min(dp[i], dp[j-1] + 1);
}
}
}

return dp[n-1];
}
};

0%