[leetcode]Palindrome Partitioning @ Python
2024-10-15 03:21:25
原题地址: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
最新文章
- 为什么目前没有";成熟";的cookie插件?
- 大话数据结构(五)(java程序)——顺序存储结构的插入与删除
- Java——Image 图片合并
- ADO.NET笔记——读取二进制大对象(BLOB)
- window2008 64位系统没有office组件问题分析及解决
- 使用blktrace排查iowait cpu高的问题
- Migrating from IntelliJ Projects
- Android开发--去掉标题栏
- 转:Visual Studio进行Web性能测试- Part III
- PA模块常用表
- Python实操
- Codeforces Round #419 (Div. 2) ABC
- glide引出恶心的git submodule
- Ubuntu Server 命令行下的默认语言改为英语en_US.UTF-8
- Spring之强制修改某个方法的行为(Arbitrary method replacement)
- MySQL使用全文索引(fulltext index)---高性能
- Linux下tar.gz 安装
- P4782 【模板】2-SAT 问题 &;&; 2-SAT问题
- Linux上的free命令简介
- string 转 java对象、转map的方式
热门文章
- 初识thinkphp(5)
- CPU的主频
- HttpServlet Service方法
- linux Shell 脚本编写
- P4811 C’s problem(c)
- Codeforces Round #371 (Div. 1) C. Sonya and Problem Wihtout a Legend 贪心
- 使用CefSharp在.Net程序中嵌入Chrome浏览器(七)——右键菜单
- STM32 CRC-32 Calculator Unit
- Golang 版本发布 与 TIOBE 排名
- 8张图理解Java---importnew---programcreek