[leetcode]Palindrome Partitioning @ Python
原题地址:https://oj.leetcode.com/problems/palindrome-partitioning/
题意:
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"]
]
解题思路:回文的分割问题。同样是需要穷举出所有符合条件的集合,那么我们还是使用dfs。
代码:
class Solution:
# @param s, a string
# @return a list of lists of string
def isPalindrome(self, s):
for i in range(len(s)):
if s[i] != s[len(s)-1-i]: return False
return True def dfs(self, s, stringlist):
if len(s) == 0: Solution.res.append(stringlist)
for i in range(1, len(s)+1):
if self.isPalindrome(s[:i]):
self.dfs(s[i:], stringlist+[s[:i]]) def partition(self, s):
Solution.res = []
self.dfs(s, [])
return Solution.res
[leetcode]Palindrome Partitioning @ Python的更多相关文章
- LeetCode:Palindrome Partitioning,Palindrome Partitioning II
LeetCode:Palindrome Partitioning 题目如下:(把一个字符串划分成几个回文子串,枚举所有可能的划分) Given a string s, partition s such ...
- [LeetCode] Palindrome Partitioning II 解题笔记
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- [leetcode]Palindrome Partitioning II @ Python
原题地址:https://oj.leetcode.com/problems/palindrome-partitioning-ii/ 题意: Given a string s, partition s ...
- [LeetCode] Palindrome Partitioning II 拆分回文串之二
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- [LeetCode] Palindrome Partitioning 拆分回文串
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- LeetCode: Palindrome Partitioning 解题报告
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- Leetcode: Palindrome Partitioning II
参考:http://www.cppblog.com/wicbnu/archive/2013/03/18/198565.html 我太喜欢用dfs和回溯法了,但是这些暴力的方法加上剪枝之后复杂度依然是很 ...
- [Leetcode] Palindrome Partitioning
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- LeetCode: Palindrome Partitioning [131]
[称号] Given a string s, partition s such that every substring of the partition is a palindrome. Retur ...
随机推荐
- Android-Activity的切换效果
Android-Activity的切换效果 Android-Activity的切换效果 Activity有一个默认的切换效果,但是有时候单一的切换效果未免单调,Activity的切换效果也是我们可以自 ...
- SDOI2013 R1 Day2
目录 2018.3.25 Test 总结 T1 BZOJ.3129.[SDOI2013]方程(扩展Lucas 容斥) T2 洛谷.3305.[SDOI2013]费用流(最大流ISAP 二分) T3 B ...
- 外键的约束(Mysql、PostgreSQL)
关于外键是什么,具体不再详述,可以自行百度. 讲一下关于外键的 On Delete和On Update的使用 最近在项目的表中看到这些,不懂顺便查了查: ONSTRAINT "c_clust ...
- 再谈JavaScript中的闭包
一.什么是闭包 闭包就是有权访问另一个函数作用域中变量的函数,因此,闭包的本质是一个函数.当一个内部函数被保存到外部时,就会生成闭包. 二.闭包的作用 1.实现公有变量,即通过局部变量实现全局变量的效 ...
- Linux/CentOS实现交换机-简单的交换机实现
个人理解: 1.交换机的核心在于Mac地址学习,使其能在全双工/半双工下进行转发工作. 2.对于专业的交换机来说,使用的是专业的网络芯片并自己实现里面的协议,比如说华为的,为什么能卖那么贵,贵在于网络 ...
- Unity IOC容器通过配置实现类型映射的几种基本使用方法
网上关于Unity IOC容器使用的方法已很多,但未能做一个总结,故我这里总结一下,方便大家选择. 首先讲一下通过代码来进行类型映射,很简单,代码如下 unityContainer = new Uni ...
- USBDM RS08/HCS08/HCS12/Coldfire V1,2,3,4/DSC/Kinetis Debugger and Programmer -- MC9S08JS16
Introduction The attached files provide a port of a combined TBDML/OSBDM code to a MC9S08JS16 proces ...
- FindWindow用法
函数功能:该函数获得一个顶层窗口的句柄,该窗口的类名和窗口名与给定的字符串相匹配.这个函数不查找子窗口.在查找时不区分大小写. 函数型:HWND FindWindow(LPCTSTR IpClassN ...
- 北大 ACM 分类 汇总
1.搜索 //回溯 2.DP(动态规划) 3.贪心 北大ACM题分类2009-01-27 1 4.图论 //Dijkstra.最小生成树.网络流 5.数论 //解模线性方程 6.计算几何 //凸壳.同 ...
- Ubuntu上的Hadoop安装教程
Install Hadoop 2.2.0 on Ubuntu Linux 13.04 (Single-Node Cluster) This tutorial explains how to insta ...