题目来源:

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

 class Solution(object):
def partition(self, s):
"""
:type s: str
:rtype: List[List[str]]
"""
m = len(s)
if m == 0:
return []
def isp(i,j):
while i < j:
if s[i] != s[j]:
return False
i += 1
j -= 1
return True
def solve(i):
ans = []
if i == m - 1:
return [[s[i]]]
j = i
while j < m:
if isp(i,j):
tmp = solve(j + 1)
if len(tmp) == 0:
ans.append([s[i:j + 1]])
else:
for k in tmp:
ans.append([s[i:j + 1]] + k)
j += 1
return ans
return solve(0)

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

  1. leetcode 131. Palindrome Partitioning 、132. Palindrome Partitioning II

    131. Palindrome Partitioning substr使用的是坐标值,不使用.begin()..end()这种迭代器 使用dfs,类似于subsets的题,每次判断要不要加入这个数 s ...

  2. Leetcode 22. Generate Parentheses Restore IP Addresses (*) 131. Palindrome Partitioning

    backtracking and invariant during generating the parathese righjt > left  (open bracket and cloas ...

  3. 【LeetCode】131. Palindrome Partitioning 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...

  4. [LeetCode] 131. Palindrome Partitioning 回文分割

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  5. Leetcode 131. Palindrome Partitioning

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  6. [leetcode]131. Palindrome Partitioning字符串分割成回文子串

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  7. 【LeetCode】131. Palindrome Partitioning

    Palindrome Partitioning Given a string s, partition s such that every substring of the partition is ...

  8. Java for LeetCode 131 Palindrome Partitioning

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  9. 131. Palindrome Partitioning

    题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...

  10. 78. Subsets(M) & 90. Subsets II(M) & 131. Palindrome Partitioning

    78. Subsets Given a set of distinct integers, nums, return all possible subsets. Note: The solution ...

随机推荐

  1. Mahout源码MeanShiftCanopyDriver分析之二MeanShiftCanopyMapper仿造

    首先更正一点,昨天处理数据的时候是有问题的,直接从网页中拷贝的文件的空格是有问题的,直接拷贝然后新建的文件中的空格可能有一个两个.三个的,所以要把两个或者三个的都换为一个,在InputMapper中下 ...

  2. B树、B-树、B+树、B*树都是什么(转)

    B树        即二叉搜索树:        1.所有非叶子结点至多拥有两个儿子(Left和Right):        2.所有结点存储一个关键字:        3.非叶子结点的左指针指向小于 ...

  3. jQuery Mobile基础

    1.安装 在<head></head>标签里边写入以下内容 jQuery Mobile CDN: <head> <meta charset="utf ...

  4. 【android】两个按钮的宽度各占屏幕的一半

    <LinearLayout> <Button android:layout_height="wrap_content" android:layout_width= ...

  5. collectionView 中cell间距设置建议

    应该是调节UICollectionViewFlowLayout的minimumInteritemSpacing属性,这个是调节同一行的cell之间的距离的. 使用-(CGFloat )collecti ...

  6. JDK源码学习--String篇(三) 存储篇

    在进一步解读String类时,先了解下内存分配和数据存储的. 数据存储 1.寄存器:最快的存储区,位于处理器的内部.由于寄存器的数量有限,所以寄存器是按需分配. 2.堆栈:位于RAM中,但是通过堆栈指 ...

  7. hibernate sql查询后对象转换成实体类

    在多表查询的时候使用hibernate的sql查询的时候,一般返回的是object[]数组,或者可以使用  session.createSQLQuery(sql).setResultTransform ...

  8. Convert Sorted Array to Balanced Binary Search Tree (BST)

    (http://leetcode.com/2010/11/convert-sorted-array-into-balanced.html) Given an array where elements ...

  9. QML Performance

    1) Limit JavaScript a) inline JavaScript:  内联的JavaScript方法;  1. 将js方法放置在Element内部; 2. 尝试将语句写在一行内; e. ...

  10. JavaScript中的闭包理解

    原创文章,转载请注明:JavaScript中的闭包理解  By Lucio.Yang 1.JavaScript闭包 在小学期开发项目的时候,用node.js开发了服务器,过程中遇到了node.js的第 ...