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","b"]
  ]

[解题思路]

动态规划,dp[i][j] = s[i]==s[j]&&(j-i<2||dp[i+1][j-1])

 1 vector<vector<string>> Solution::partition(string s){
 2     const int len = s.length();
 3     bool dp[len][len];
 4     fill_n(&dp[0][0], len*len, false);
 5     for (int i = len-1; i >=0; i --){
 6         for (int j = i; j < len; j++){
 7             dp[i][j] = s[i]==s[j]&&(j-i<2||dp[i+1][j-1]);
 8         }
 9     }
10     vector<vector<string>> ans[len];
11     for (int i = len-1; i>=0; i--){
12         for (int j = i; j < len; j++){
13             if (dp[i][j] == 1){
14                 const string tmp = s.substr(i, j-i+1);
15                 if (j + 1 >= len){
16                     ans[i].push_back(vector<string> {tmp});
17                 }
18                 else{
19                     for (auto k : ans[j + 1]){
20                         k.insert(k.begin(), tmp);
21                         ans[i].push_back(k);
22                     }
23                 }
24             }
25         }
26     }
27     return ans[0];
28 }
时间: 2024-10-31 22:48:58

leetcode -- Palindrome Partitioning的相关文章

[leetcode]Palindrome Partitioning @ Python

原题地址:https://oj.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 [

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

[LeetCode] Palindrome Partitioning II (DP)

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: 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 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

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 pa