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","b"]
  ]
解题思路一:

将s分为左右两个部分,分别求出两边的partition,然后粘一块即可,JAVA实现如下:

	static public List<List<String>> partition(String s) {
		Set<List<String>> set = new HashSet<List<String>>();
		if (isPalindrome(s)) {
			List<String> alist = new ArrayList<String>();
			alist.add(s);
			set.add(alist);
		}
		for (int i = 1; i < s.length(); i++) {
			List<List<String>> left = partition(s.substring(0, i));
			List<List<String>> right = partition(s.substring(i, s.length()));
			for (List<String> aLeft : left)
				for (List<String> aRight : right) {
					List<String> alist = new ArrayList<String>(aLeft);
					alist.addAll(aRight);
					set.add(new ArrayList<String>(alist));
				}
		}
		return new ArrayList<List<String>>(set);
	}

	static boolean isPalindrome(String s) {
		int left = 0;
		int right = s.length() - 1;
		while (left < right)
			if (s.charAt(left++) != s.charAt(right--))
				return false;
		return true;
	}

结果TLE

解题思路二:

修改下思路一,从左边入手,如果左边是Palindrome,对右边求一个partition,这样求得的结果也不会重复,这样就可以AC了,JAVA实现如下:

	static public List<List<String>> partition(String s) {
		ArrayList<List<String>> list = new ArrayList<List<String>>();
		if (isPalindrome(s)) {
			List<String> alist = new ArrayList<String>();
			alist.add(s);
			list.add(alist);
		}
		for (int i = 1; i < s.length(); i++)
			if (isPalindrome(s.substring(0, i))) {
				List<String> aLeft = new ArrayList<String>();
				aLeft.add(s.substring(0, i));
				List<List<String>> right = partition(s.substring(i, s.length()));
				for (List<String> aRight : right) {
					List<String> alist = new ArrayList<String>(aLeft);
					alist.addAll(aRight);
					list.add(new ArrayList<String>(alist));
				}
			}
		return list;
	}

	static boolean isPalindrome(String s) {
		int left = 0;
		int right = s.length() - 1;
		while (left < right)
			if (s.charAt(left++) != s.charAt(right--))
				return false;
		return true;
	}
时间: 2024-09-28 23:05:06

Java for LeetCode 131 Palindrome Partitioning的相关文章

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"

Leetcode#131 Palindrome Partitioning

原题地址 因为要找所有的解,只能搜索+回溯了 看来数据量比较小,关于回文串的判断没有使用动态规划也可以过 代码: 1 vector<vector<string> > res; 2 3 bool palindromep(string s) { 4 int i = 0; 5 int j = s.length() - 1; 6 while (i < j && s[i] == s[j]) { 7 i++; 8 j--; 9 } 10 return i >= j;

leetcode之Palindrome Partitioning

方法一:DFS递归,判断每一个是否为回文数1,首先要有一个判断字符串是否是回文的函数.容易实现,字符串从两边同时往中间走,看字符是否相同; 2,深度优先搜索思想对字符串进行遍历.得到结果.例如,s = "abacd"; 需要对"a""ad""aba""abac""abacd"进行深度优先搜索.深度搜索的过程如下:先选"a",发现"a"是回文,则深度

[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

【算法】leetcode之 Palindrome Partitioning I&amp;II(转载)

1 Palindrome Partitioning 问题来源:Palindrome Partitioning 该问题简单来说就是给定一个字符串,将字符串分成多个部分,满足每一部分都是回文串,请输出所有可能的情况. 该问题的难度比较大,很可能第一次遇到没有思路,这很正常.下面我们一点点分析,逐步理清思路.先不考虑所有的情况,针对一个符合条件的划分,每一部分都是一个回文子串,而且各部分的长度不固定.也即每一部分都是原始字符串的一个子串,且满足回文条件.所有的划分都满足上述条件,所以这就启发我们首先判

【leetcode】Palindrome Partitioning

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&qu

【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 p

leetcode 131. Palindrome Partitioning----- 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","

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