LeetCode – Refresh – Palindrome Partitioning II

Notes:

1. If len < 2, return 0. It is not minimum length but how many cuts.

2. As it was cutting inside of string. Final result is dp[0] - 1, since there is no cut at most left.

 1 class Solution {
 2 public:
 3     int minCut(string s) {
 4         int len = s.size();
 5         if (len < 2) return 0;
 6         vector<int> dp(len+1, 0);
 7         vector<vector<bool> > rec(len, vector<bool>(len, false));
 8         for (int i = 0; i <= len; i++) dp[i] = len - i;
 9         for (int i = len-1; i >= 0; i--) {
10             for (int j = i; j < len; j++) {
11                 if (s[i] == s[j] && (j - i < 2 || rec[i+1][j-1])) {
12                     rec[i][j] = true;
13                     dp[i] = min(dp[i], dp[j+1]+1);
14                 }
15             }
16         }
17         return dp[0]-1;
18     }
19 };
时间: 2024-10-16 14:18:15

LeetCode – Refresh – Palindrome Partitioning II的相关文章

[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

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

Java for 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"

【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][JAVA] 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 – Refresh – Palindrome Partitioning I

1 class Solution { 2 public: 3 bool isP(string s) { 4 int start = 0, end = s.size()-1; 5 while (start < end) { 6 if (s[start++] != s[end--]) return false; 7 } 8 return true; 9 } 10 void getP(vector<vector<string> > &result, vector<st

[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 [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",