题目来源:

  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. 使用MD5完成自定义Person对象的加密过程

    ---恢复内容开始--- 首先:我们对自定义Person对象的加密过程所用的方法是归档写入文件的方法. 第一步:创建Person,继承于NSObject,然后在Person.h文件遵守NSCoding ...

  2. 662 - Fast Food

    描述:状态方程p[i][j]=dp[i-1][k]+dist(k+1,j),由于没搞懂距离dist是怎么计算的,以为是num[j]-num[k+1],结果wa了一次,在状态转移的时候,采用一个数组sc ...

  3. 使用my97datepicker控件实现日期范围选择

    注:(2014-12-05内容修改:添加运行效果) 使用my97datepicker 控件,需要对日期的范围进行控制,本人自己写了一个js完成此功能,示例为当前日期到下一周周五之间的日期可选,其他日期 ...

  4. ORACLE 查找字段在哪些表里存在

    查找不是主键的字段在哪些表里存在: select owner, table_namefrom dba_tab_columnswhere lower(column_name)='firstname'; ...

  5. C++ this 指针

    类的(非静态)成员函数具有一个附加的隐含形参,即指向该类对象的一个指针.这个隐含形参命名为this,与调用成员函数的对象绑定在一起.成员函数不能定义this形参,而是由编译器隐含地定义.成员函数的函数 ...

  6. latex 常用小结

    在写论文,甚至有些课程的报告的时候,latex是常用的工具.这篇博文简单的记录了latex常用的一些内容. 1 基本模块 没用过latex的读者,最想问的问题莫过于latex的 “hello worl ...

  7. get the execution time of a sql statement.

    declare @d datetimeset @d = GETDATE()select * from dbo.spt_valuesselect [语句执行花费时间(毫秒)]= DATEDIFF(ms, ...

  8. QtSoap开发web services客户端程序

        首先需要下载QtSoap开源包,下载地址为: http://www.filestube.com/q/qtsoap+download, 我使用的是:qtsoap-2.6-opensource(不 ...

  9. 一、ThinkPHP的介绍

    一.ThinkPHP的介绍 //了解 MVC M - Model 模型 工作:负责数据的操作 V - View 视图(模板) 工作:负责前台页面显示 编写html代码 C - Controller 控 ...

  10. CCNA实验(3) -- RIP

    RIP协议分为版本1和版本2,均具备以下特征:1.是距离向量路由协议2.使用跳数(Hop Count)作为度量值3.默认路由更新周期为30秒4.管理距离(AD)为1205.支持触发更新6.最大跳数为1 ...