leetcode--Palindrome Partitioning
来源:互联网 发布:p2p网络理财规划 编辑:程序博客网 时间:2024/06/05 04:18
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"] ]
public class Solution { public List<List<String>> partition(String s) {ArrayList<List<String>> res = new ArrayList<List<String>>();if(s.length()==0) return res;boolean flag[][] = new boolean[s.length()][s.length()];for(int i=0;i<s.length();i++){flag[i][i] = true;}for(int i=s.length()-1;i>=0;i--){for(int j=i+1;j<s.length();j++){if(s.charAt(i)==s.charAt(j)){if(j-i==1){flag[i][j] = true;}else{flag[i][j] = flag[i+1][j-1];}}else{flag[i][j] = false;}}}dfs(res, flag, new ArrayList<String>(), 0, s);return res; } void dfs(ArrayList<List<String>> res,boolean[][] flag,List<String> item,int start,String s){if(start==flag.length){res.add(new ArrayList<String>(item));}else{for(int i=start+1;i<flag.length+1;i++){if(flag[start][i-1]){item.add(s.substring(start, i));dfs(res, flag, item, i, s);item.remove(item.size()-1);}}}}}
0 0
- 【leetcode】Palindrome Partitioning && Palindrome Partitioning II
- [LeetCode] Palindrome Partitioning && Palindrome Partitioning II
- LeetCode Palindrome Partitioning I&&Palindrome Partitioning II
- LeetCode Palindrome Partitioning, Palindrome Partitioning II
- [Leetcode][python]Palindrome Partitioning/Palindrome Partitioning II
- Leetcode:Palindrome Partitioning & Palindrome Partitioning II
- LeetCode: Palindrome Partitioning
- LeetCode : Palindrome Partitioning II
- [leetcode] Palindrome Partitioning II
- leetcode 122: Palindrome Partitioning
- leetcode:Palindrome Partitioning II
- leetcode:Palindrome Partitioning
- 【leetcode】Palindrome Partitioning
- 【leetcode】Palindrome Partitioning II
- leetcode - Palindrome Partitioning II
- Leetcode - Palindrome Partitioning
- LeetCode之Palindrome Partitioning
- [LeetCode]Palindrome Partitioning
- 目前的几种应用防杀方式
- Javascript 严格模式详解
- cuda之一步一步爬
- Object.defineproperty实现数据和视图的联动
- scrapy爬豆瓣电影
- leetcode--Palindrome Partitioning
- Linux学习笔记(五)--SSH
- 专访曾宪杰:大型网站系统与Java中间件实践
- 【猫猫的Unity Shader之旅】之Lambert光照
- GO GO GO
- Linux/Ubuntu学习笔记(六)--JDK安装及一个java例程
- leetcode刷题,总结,记录,备忘 226
- ListView上拉加载更多(分页加载)
- Linux/Ubuntu学习笔记(六)--tomcat