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","b"]
]

Keys: 1. 一般列出所有组合,可能性,etc 的题目都可以用DFS.

2. L8. 对于Class Solution中的全局变量x,可以用Solution.x

 class Solution(object):
def partition(self, s):
"""
:type s: str
:rtype: List[List[str]]
"""
Solution.res = []
self.DFS(s, [])
return Solution.res def DFS(self, s, stringlist):
if len(s) == 0:
Solution.res.append(stringlist)
# 如果一开始的substring是palin,把他存到stringlist中
for i in range(1, len(s)+1):
if self.isPalin(s[:i]):
self.DFS(s[i:], stringlist + [s[:i]]) def isPalin(self, s):
for i in range(len(s)):
if s[i] != s[len(s)-1-i]:
return False
return True


Leetcode 131. Palindrome Partitioning的更多相关文章

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

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

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

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

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

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

  4. Java for LeetCode 131 Palindrome Partitioning

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

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

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

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

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

  7. 【LeetCode】131. Palindrome Partitioning

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

  8. 131. Palindrome Partitioning

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

  9. 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. Use Cursor

    declare : CURSOR cursor_name IS select_statement ; open : OPEN cursor_name if the query returns no r ...

  2. datepicker monthpicker

  3. 从浏览器输入url到页面加载完成都发生了什么

    一个http请求的过程 简要介绍一下一个http请求的网络传输过程: DNS Lookup先获得URL对应的IP地址 Socket Connect浏览器和服务器建立TCP连接 Send Request ...

  4. npm中package.json详解

    通常我们使用npm init命令来创建一个npm程序时,会自动生成一个package.json文件.package.json文件会描述这个NPM包的所有相关信息,包括作者.简介.包依赖.构建等信息,格 ...

  5. web文档在线阅览

    之前遇到很多各种文档在线阅览的需求,也有不少朋友经常问我这种需求的实现方案,大致试了一下网上的一些比较主流的推荐方案,但都不尽如人意,这里有一个比较全面的总结,需要的朋友可以根据自己的需求到这里查看, ...

  6. 关于.Net的面试遐想

    概述 这几天更新相关的面试题目,主是要针对有4年或以上经验的面试者,总体来说,发现面试人员的答题效果和预期相差比较大,我也在想是不是我出的题目偏离现实,但我更愿意相信,是我们一些.Net开发者在工作中 ...

  7. Java语法笔记

    目录 知识点 不支持 恶心事 与C#的区别 组件 学习资料 母版页 知识点 类 静态方法,即可以在类上被调用,也可以在实例对象上被调用. Java类 先执行静态构造函数,再执行静态方法或静态字段,所以 ...

  8. Sentinel-Redis高可用方案(二):主从切换

    Redis 2.8版开始正式提供名为Sentinel的主从切换方案,Sentinel用于管理多个Redis服务器实例,主要负责三个方面的任务:     1. 监控(Monitoring): Senti ...

  9. spring配置属性的两种方式

    spring配置属性有两种方式,第一种方式通过context命名空间中的property-placeholder标签 <context:property-placeholder location ...

  10. tomcat设置端口号和默认webapp

    tomcat一下载,解压之后webapps目录下自带几个webapp: * docs文档:这是一个静态页面集,不用启动tomcat也可以阅读 * examples样例 * hostmanager主机管 ...