[Lintcode]136. Palindrome Partitioning /[Leetcode]131. Palindrome Partitioning
生活随笔
收集整理的這篇文章主要介紹了
[Lintcode]136. Palindrome Partitioning /[Leetcode]131. Palindrome Partitioning
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
136. Palindrome Partitioning / 131. Palindrome Partitioning
- 本題難度: Medium
Topic: Search DFS
Description
Given a string s, partition s such that every substring of the partition is a palindrome.
Return all possible palindrome partitioning of s.
Example
Given s = "aab", return:
[
["aa","b"],
["a","a","b"]]
我的代碼
recursion
class Solution:"""@param: s: A string@return: A list of lists of string"""def partition(self, s):# write your code heredef dfs(s,part, res):if s == '':res.append(part)return for i in range(1,len(s)+1):if isValid(s[:i]):dfs(s[i:],part+[s[:i]],res)def isValid(s):l = len(s)for i in range(l//2):if s[i]!=s[l-1-i]:return Falsereturn Trueres = []dfs(s, [], res)return res思路
非常常見的DFS.
res為全局變量。所以不需要return什么值
轉載于:https://www.cnblogs.com/siriusli/p/10392217.html
總結
以上是生活随笔為你收集整理的[Lintcode]136. Palindrome Partitioning /[Leetcode]131. Palindrome Partitioning的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: springcloud(五) Hyst
- 下一篇: laravel上传到七牛图片插件