原题地址: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的更多相关文章

  1. LeetCode:Palindrome Partitioning,Palindrome Partitioning II

    LeetCode:Palindrome Partitioning 题目如下:(把一个字符串划分成几个回文子串,枚举所有可能的划分) Given a string s, partition s such ...

  2. [LeetCode] Palindrome Partitioning II 解题笔记

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

  3. [leetcode]Palindrome Partitioning II @ Python

    原题地址:https://oj.leetcode.com/problems/palindrome-partitioning-ii/ 题意: Given a string s, partition s  ...

  4. [LeetCode] Palindrome Partitioning II 拆分回文串之二

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

  5. [LeetCode] Palindrome Partitioning 拆分回文串

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

  6. LeetCode: Palindrome Partitioning 解题报告

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

  7. Leetcode: Palindrome Partitioning II

    参考:http://www.cppblog.com/wicbnu/archive/2013/03/18/198565.html 我太喜欢用dfs和回溯法了,但是这些暴力的方法加上剪枝之后复杂度依然是很 ...

  8. [Leetcode] Palindrome Partitioning

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

  9. LeetCode: Palindrome Partitioning [131]

    [称号] Given a string s, partition s such that every substring of the partition is a palindrome. Retur ...

随机推荐

  1. oracle 编码

    select * from nls_database_parameters where parameter ='NLS_CHARACTERSET'; PARAMETER VALUE --------- ...

  2. Android对Sqlite数据库的增删改查

    SqLite 数据库 Google 为我们提供了sqlite相关的api SqLiteOpenHelper 这是一个抽象的类 如果想要使用的话,需要其他的类去继承他 SqLiteDatabase 类 ...

  3. 优美的爆搜?KDtree学习

    如果给你平面内一些点,让你求距离某一个指定点最近的点,应该怎么办呢? O(n)遍历! 但是,在遍历的过程中,我们发现有一些点是永远无法更新答案的. 如果我们把这些点按照一定顺序整理起来,省略对不必要点 ...

  4. 图的基本操作(基于邻接表):图的构造,深搜(DFS),广搜(BFS)

    #include <iostream> #include <string> #include <queue> using namespace std; //表结点 ...

  5. Codeforces Round #374 (Div. 2) C. Journey DP

    C. Journey 题目连接: http://codeforces.com/contest/721/problem/C Description Recently Irina arrived to o ...

  6. 使用 IntraWeb (13) - 基本控件之 TIWLabel、TIWLink、TIWURL、TIWURLWindow

    TIWLabel // TIWLink //内部链接 TIWURL //外部链接 TIWURLWindow //页内框架, 就是 <iframe></iframe> TIWLa ...

  7. Linux下Shell函数返回值实现种类

    shell在执行的时候是顺序执行的,也不存在什么多线程什么的. 一下是实现种类: 1.全局 g_result="" function testFunc() { g_result=' ...

  8. ISO 7816-4: GET RESPONSE and ENVELOPE command

    http://www.cardwerk.com/smartcards/smartcard_standard_ISO7816-4_7_transmission_interindustry_command ...

  9. 【防火墙技术连载11】强叔侃墙 攻击防范篇 流量型攻击之UDP Flood及防御

    http://support.huawei.com/huaweiconnect/enterprise/thread-214141.html

  10. kcon 黑客大会 github

    https://github.com/knownsec/KCon https://paper.seebug.org/107/ CISP认证 https://habo.qq.com/ https://g ...