Java-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"]
  ]

将给定字符串进行分解 使每一个分部都是回文结构 要求给出所有可能的组合

典型的回溯法求解  每次从第index位开始拆分 i=(index,s.length()-1) 若index-i位满足回文结构 则从第i+1位继续递归 需要注意的是 结束条件可以是index=s.length()即所有位都拆分完毕 代码如下:

public class Solution {

    public  List<List<String>> partition(String s) {
		List<List<String>> res=new ArrayList<List<String>>();
		List<String>  tmp=new ArrayList<String>();
		part(res, s, tmp, 0);
		return res;
    }
	public   void part( List<List<String>> res,String s,List<String>  tmp,int index){
		if(index==s.length()){
			res.add(tmp);
			return;
		}
		for(int i=index;i<s.length();i++){
			if(isPalindrome(s, index, i)){
				List<String>  ntmp=new ArrayList<String>();
				ntmp.addAll(tmp);
				ntmp.add(s.substring(index,i+1));
				part(res, s, ntmp, i+1);
			}
		}
	}
	public  boolean isPalindrome(String s,int start,int end){
		for(int i=0;i<(end-start+1)/2;i++){
			if(s.charAt(i+start)!=s.charAt(end-i)){
				return false;
			}
		}
		return true;
	}

}

时间: 2024-11-08 19:12:33

Java-Palindrome Partitioning的相关文章

[LeetCode][Java] 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",&

[Leetcode][JAVA] Palindrome Partitioning II

Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s. For example, given s = "aab",Return 1 since the palindrome partitioning ["aa",

Java for LeetCode 132 Palindrome Partitioning II

Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s. For example, given s = "aab", Return 1 since the palindrome partitioning ["aa"

Palindrome Partitioning leetcode java

题目: 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",&

Java for LeetCode 131 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",

Palindrome Partitioning II Leetcode java

题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s. For example, given s = "aab", Return 1 since the palindrome partitioning ["aa&q

LeetCode: Palindrome Partitioning II 解题报告

Palindrome Partitioning II Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s. For example, given s = "aab",Return 1 since the palindrome pa

LeetCode: 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",

LeetCode 131. 分割回文串(Palindrome Partitioning)

131. 分割回文串 131. Palindrome Partitioning 题目描述 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. LeetCode131. Palindrome Partitioning中等 示例: 输入: "aab" 输出: [ ??["aa","b"], ??["a","a","b"]] Java 实现 略

[LeetCode]132.Palindrome Partitioning II

题目 Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s. For example, given s = "aab", Return 1 since the palindrome partitioning ["aa&qu