LeetCode PalindromePartitioning

题目描述:https://oj.leetcode.com/problems/palindrome-partitioning/

给定一个字符串,要求求出该串的所有回文分割方式。其实就是对该字符串的划分树进行深搜。

package PalindromePartitioning;

import java.util.*;
public class Solution {
	boolean isPalindrome(String s){
		if(s.length() == 1)
			return true;
		else
		{
			int i = 0,j = s.length()-1;
			for(;s.charAt(i)==s.charAt(j)&&i<=j;i++,j--);
			if(i>j) return true;
			else return false;
		}
	}
	void dfs(String s,int start,List<String> ans,List<List<String>> res)
	{
		if(start==s.length()){
			res.add(new LinkedList<String>(ans));
			return;
		}
		for(int i=start+1;i<=s.length();i++){
			String temp = s.substring(start, i);
			if(isPalindrome(temp)==true){
				ans.add(temp);
				dfs(s,i,ans,res);
				ans.remove(ans.size()-1);
			}
		}
	}
    public List<List<String>> partition(String s) {
    	List<List<String>> res = new LinkedList<List<String>>();
    	List<String> ans = new LinkedList<String>();
    	dfs(s,0,ans,res);
        return res;
    }
    
    //for test
    public static void main(String[] args){
    	List<List<String>> ans = new Solution().partition("aabaa");
    	for(List<String> l :ans)
    	{
    		for(String str:l)
    			System.out.print(str+" ");
    		System.out.println("");
    	}
    }
}


你可能感兴趣的:(LeetCode PalindromePartitioning)