leetcode 131. Palindrome Partitioning回文子串+DFS
来源:互联网 发布:牛乳のみお js 编辑:程序博客网 时间:2024/05/29 04:35
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"]]
import copyclass Solution(object): def partition(self, s): self.s = s n = len(s) self.n = n self.dp = [([False]*n) for i in range(0 , n)] for r in range(0 , n): for l in range(0 , r+1): if s[l] == s[r] and ((r - l <= 2) or self.dp[l+1][r-1]): self.dp[l][r] = True self.reslut = [] self.dfs(-1 , []) return self.reslut def dfs(self, father , h): if father == self.n - 1 : self.reslut.append(copy.copy(h)) return for i in range(father+1 , self.n): if self.dp[father+1][i]: h.append(self.s[father+1:i+1]) self.dfs(i , h) h.pop()if __name__ == '__main__': s = Solution() ; print(s.partition("aab"))
JAVA版
import java.util.ArrayList;import java.util.Arrays;import java.util.LinkedList;import java.util.List;public class Solution {boolean[][] dp ;String word ;int n ;List<List<String>> result ;public List<List<String>> partition(String s) { n = s.length() ; word = s ; dp = new boolean[n][n] ; for(int i = 0 ; i < n ; i++){ Arrays.fill(dp[i] , false) ; } for(int r = 0 ; r < n ; r++){ for(int l = 0 ; l <= r ; l++){ if(word.charAt(l) == word.charAt(r) && ((r - l <= 2) || dp[l+1][r-1])){ dp[l][r] = true ; } } } result = null ; dfs(-1 , new LinkedList<String>()) ; return result ; }void dfs(int father , LinkedList<String> h){if(father == n-1){if(result == null){result = new ArrayList<List<String>>() ;}result.add(new ArrayList<String>(h)) ;return ; }for(int i = father + 1 ; i < n ; i++){if(dp[father+1][i]){h.addLast(word.substring(father+1 , i+1)) ; dfs(i , h) ;h.removeLast() ;}}}public static void main(String[] args) {Solution s = new Solution() ;s.partition("aab") ;System.out.println(s.result) ;}}
阅读全文
0 0
- leetcode 131. Palindrome Partitioning回文子串+DFS
- leetcode 132. Palindrome Partitioning II 回文子串 + DFS + DP + 这道题需要认真学习
- LeetCode------------palindrome-partitioning(回文)
- 回文子串划分 Palindrome Partitioning
- leetcode dfs Palindrome Partitioning
- 【LeetCode】131.Palindrome Partitioning回文划分
- LeetCode 131. Palindrome Partitioning(回文分区)
- [LeetCode]--131. Palindrome Partitioning(backTracking && DFS && DP)
- [LeetCode] Palindrome Partitioning 回文分割
- LeetCode | Palindrome Partitioning(将一个子串划分成回文子串)
- LeetCode | Palindrome Partitioning II(最少切割次数将一个子串划分成回文子串)
- [C++]LeetCode: 121 Palindrome Partitioning (分割回文子串 回溯法)
- leetcode -- Palindrome Partitioning -- 重点 dfs
- LeetCode 131 Palindrome Partitioning (DFS)
- leetcode---palindrome-partitioning---dp, dfs
- Palindrome Partitioning 分割成回文子字符串
- LeetCode 131. Palindrome Partitioning
- [LeetCode]131.Palindrome Partitioning
- 备份mysql数据库(例:mac)
- wamp环境搭建
- ZUFE 2469 无人能解得数学题【矩阵快速幂】
- 浪漫的平安夜,听一听小白的年终总结(二)之 2016
- iOS 搭建框架简述 —— HERO博客
- leetcode 131. Palindrome Partitioning回文子串+DFS
- [cogs2692]天才魔法少女琪露诺爱计数
- 远程凭据无法工作解决办法
- SSM(SpringMvc+mybatis+Spring)java web搭建工程
- linux deepin系统 系列(一)安装JDK
- mysql 权限与安全
- AngularJS 教程(二)
- 外网访问内网服务器
- Pick apples(山东省第三届ACM大学生程序设计竞赛)