[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):
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的更多相关文章
- leetcode 131. Palindrome Partitioning 、132. Palindrome Partitioning II
131. Palindrome Partitioning substr使用的是坐标值,不使用.begin()..end()这种迭代器 使用dfs,类似于subsets的题,每次判断要不要加入这个数 s ...
- Leetcode 22. Generate Parentheses Restore IP Addresses (*) 131. Palindrome Partitioning
backtracking and invariant during generating the parathese righjt > left (open bracket and cloas ...
- 【LeetCode】131. Palindrome Partitioning 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...
- [LeetCode] 131. Palindrome Partitioning 回文分割
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- Leetcode 131. Palindrome Partitioning
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- [leetcode]131. Palindrome Partitioning字符串分割成回文子串
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- 【LeetCode】131. Palindrome Partitioning
Palindrome Partitioning Given a string s, partition s such that every substring of the partition is ...
- Java for LeetCode 131 Palindrome Partitioning
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- 131. Palindrome Partitioning
题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...
- 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 ...
随机推荐
- 多个target下编译的时候出错问题的解决
在工程里如果有多个target的时候,如图 那么编译的时候一定要注意Xcode右侧勾选了正确的target,否则有可能会导致一系列让你想不到的bug. ,另外,如果工程中有framework,那么一定 ...
- java 请求响应乱码
package org.operamasks.servlet; import java.io.IOException; import java.io.PrintWriter; import java. ...
- IHttpModule与IHttpHandler的区别整理
IHttpModule与IHttpHandler的区别整理1.先后次序.先IHttpModule,后IHttpHandler. 注:Module要看你响应了哪个事件,一些事件是在Handler之前运行 ...
- Fix Some bytes have been replaced with the Unicode substitution character while loading file XXX.cs with Chinese Simplified (GB2312) encoding
When we use <strong>visual studio</strong> open source file or any other file, we may en ...
- asp.net 向后台提交 html 代码段 包括 <> 标签
首先 在默认情况向标签类的东西是不会让你提交的 这是出于.net 的默认安全机制 我们要先在 <%@ page %> 里边加上 ValidateRequest="false&q ...
- Win8.1重装win7或win10中途无法安装
一.有的是usb识别不了,因为新的机器可能都是USB3.0的,安装盘是Usb2.0的. F12更改系统BIOS设置,我改了三个地方: 1.设置启动顺序为U盘启动 2.关闭了USB3.0 control ...
- SQL Server存储过程和游标有关实例以及相关网址
内含游标的存储过程实例 第一种写法 GO BEGIN IF (object_id('PT_FAULT_REPORT', 'P') is not null) drop proc PT_FAULT_REP ...
- mlpack:可伸缩C++机器学习库(转)
摘要:mlpack是一个可伸缩C++机器学习库,它的目的是让新用户通过简单.一致的API使用机器学习,同时为专业用户提供C++的高性能和最大灵活性. mlpack是一个直观.快速.可伸缩的C++机器学 ...
- PHP图片裁剪函数(图像不变形)
PHP图片裁剪函数(图像不变形) <? *exif_imagetype -- 判断一个图像的类型 *说明:函数功能是把一个图像裁剪为任意大小的图像,图像不变形 * 参数说明:输入 需要处理图片的 ...
- 个人笔记mysql游标
经过测试,mysql游标是无法读取自定义函数计算的结构,mysql自带的函数计算值是可以读取的.