题目来源:

  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. Ubuntu小私房(3)--Uubutnu启动美化大变身

    Grub是什么? GNU GRUB 和GRUB是GRand Unified Bootloader的缩写,它是一个多重操作系统启动管理器.用来引导不同系统,如windows,linux.GRUB是多启动 ...

  2. 从零单排PAT1015,1016,1017,1018

    1015德才论 题目要求: 输入格式: 输入第1行给出3个正整数,分别为:N(<=105),即考生总数.L(>=60).为录取最低分数线,即德分和才分均不低于L的考生才有资格被考虑录取:H ...

  3. CF Codeforces Round #258 (Div. 2) B (451B)

    题意:找出一段逆序! 预存a[]数组到b[]数组.将b排序,然后前后找不同找到区间[l,r],然后推断[l,r]是否逆序就能够了!.当然还得特判本身就是顺序的!! ! AC代码例如以下: #inclu ...

  4. java 对象数组定义

    下面代码实现了定义一个数组对象 public class Student { private String username; private int num; public Student(Stri ...

  5. IO库 8.5

    题目:重写8.4中的函数,将每一个单词作为一个独立的元素进行存储. #include <iostream> #include <fstream> #include <st ...

  6. 5.7.1.4 window对象

    ECMAScript虽然没有指出如何直接访问Global对象,但web浏览器都是将这个全局对象作为window对象的一部分加以实现的.因此,在全局作用域中声明的所有变量和函数,就都成为了window对 ...

  7. JS兼容性问题列表

    记录平时遇见的兼容性问题,有更好的解决办法希望各位提出,会随着开发遇到问题而更新,标记为黄色的为未解决和猜测答案 提出时间 问题描述 解决方案 2014/10/22 submit按钮阻止了默认事件不能 ...

  8. JS中的this都有什么作用?

    1.全局代码中的this  是指向全局对象,在浏览器中是window alert(this) //window 2.作为单纯的函数调用: function fooCoder(x) { this.x = ...

  9. less 工具

    less 工具也是对文件或其它输出进行分页显示的工具,应该说是linux正统查看文件内容的工具,功能极其强大.less 的用法比起 more 更加的有弹性.在 more 的时候,我们并没有办法向前面翻 ...

  10. zoj 2071 Technology Trader 最大权闭合子图

    传送门 和上一题一样, 也是一个最大权闭合子图.不过建图好麻烦的感觉  写了好久. 源点和原材料连边, 权值为val. 汇点和产品连边, 权值为val. 产品与和它有关系的材料连边, 权值inf. 最 ...