[LeetCode]题解(python):131-Palindrome Partitioning

题目来源:

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



题意分析:

  给定一个字符串s,将s拆成若干个子字符串,使得所有的子字符串都是回文字符串,返回所有这样的子字符串集合。比如s = “aab”,那么返回[["aa","b"],["a","a","b"]]。



题目思路:

  这是一个动态规划问题,如果s[:i]是回文字符串,那么s[:i] X solve(s[i+1:]),(X是笛卡尔乘积,solve(s[i+1:])是s[i+1:]的答案)。所以只需要判断s[:i]是不是回文就可以了。



代码(python):

 1 class Solution(object):
 2     def partition(self, s):
 3         """
 4         :type s: str
 5         :rtype: List[List[str]]
 6         """
 7         m = len(s)
 8         if m == 0:
 9             return []
10         def isp(i,j):
11             while i < j:
12                 if s[i] != s[j]:
13                     return False
14                 i += 1
15                 j -= 1
16             return True
17         def solve(i):
18             ans = []
19             if i == m - 1:
20                 return [[s[i]]]
21             j = i
22             while j < m:
23                 if isp(i,j):
24                     tmp = solve(j + 1)
25                     if len(tmp) == 0:
26                         ans.append([s[i:j + 1]])
27                     else:
28                         for k in tmp:
29                             ans.append([s[i:j + 1]] + k)
30                 j += 1
31             return ans
32         return solve(0)
33
34                     

时间: 2024-07-30 23:52:56

[LeetCode]题解(python):131-Palindrome Partitioning的相关文章

leetcode题解:Valid Palindrome(判断回文)

题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example,"A man, a plan, a canal: Panama" is a palindrome."race a car" is not a palindrome. Note:Have you consider tha

leetcode第一刷_ Palindrome Partitioning II

这道题还挺复杂的,回来看了好一会儿才想起当时怎么想的..上道题刚说不要打表,这道题就用了打表.. 总的思路是这样的,从后面往前面打表,最后一个位置的最小分割一定是0,那往前呢,如果当前考虑的位置是start,并且substr(s, i)是回文的,那么如果已知i+1开始的分割次数,那么start这个位置的分割应该就是start原来的和i+1开始的分割次数加1之间的最小值.DP的思想,很直接. 但是我忽略了一个另一个开销很大的地方,那就是每次判断回文的时候.这个跟单词分割还不一样,这个范围很大.最后

leetcode -day11 Clone Graph &amp; Palindrome Partitioning I II

 1.Clone Graph Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's undirected graph serialization: Nodes are labeled uniquely. We use # as a separator for each node, and , as a separator for node lab

Java for LeetCode 131 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",

131. 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","

[leedcode 131] 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","

131. 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","

[LC] 131. 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. Example: Input: "aab" Output: [ ["aa","b"], ["a","a","b"

Leetcode#131 Palindrome Partitioning

原题地址 因为要找所有的解,只能搜索+回溯了 看来数据量比较小,关于回文串的判断没有使用动态规划也可以过 代码: 1 vector<vector<string> > res; 2 3 bool palindromep(string s) { 4 int i = 0; 5 int j = s.length() - 1; 6 while (i < j && s[i] == s[j]) { 7 i++; 8 j--; 9 } 10 return i >= j;

131 Palindrome Partitioning 分割回文串

给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串.返回 s 所有可能的分割方案.例如,给出 s = "aab",返回[  ["aa","b"],  ["a","a","b"]]详见:https://leetcode.com/problems/palindrome-partitioning/description/ class Solution { public: vect