每日AC--leetCode--PalindromePartitioning--回溯法 递归实现
2017-09-15 10:16
381 查看
每日AC--leetCode--PalindromePartitioning--回溯法 递归实现
题目描述
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"]
AC 代码:
/**
* 版权所有 (c) 2017,xiaoming有限公司
*/
package algorithm;
import java.util.ArrayList;
/**
* 类说明
*
* <pre>
* Modify Information:
* Author Date Description
* ============ =========== ============================
* DELL 2017年9月15日 Create this file
* </pre>
*
*/
public class LeetCodePalindromePartitioning {
// 看似字符串切分,实则可以考虑回溯法实现
/**
* 先看下回溯法实现 递归与非递归
int a
,i;
2: 初始化数组a[];
3: i = 1;
4: while (i>0(有路可走) and (未达到目标)) // 还未回溯到头
5: {
6: if(i > n) // 搜索到叶结点
7: {
8: 搜索到一个解,输出;
9: }
10: else // 处理第i个元素
11: {
12: a[i]第一个可能的值;
13: while(a[i]在不满足约束条件且在搜索空间内)
14: {
15: a[i]下一个可能的值;
16: }
17: if(a[i]在搜索空间内)
18: {
19: 标识占用的资源;
20: i = i+1; // 扩展下一个结点
21: }
22: else
23: {
24: 清理所占的状态空间; // 回溯
25: i = i –1;
26: }
27: }
递归实现:
int a
;
2: try(int i)
3: {
4: if(i>n)
5: 输出结果;
6: else
7: {
8: for(j = 下界; j <= 上界; j=j+1) // 枚举i所有可能的路径
9: {
10: if(fun(j)) // 满足限界函数和约束条件
11: {
12: a[i] = j;
13: ... // 其他操作
14: try(i+1);
15: 回溯前的清理工作(如a[i]置空值等);
16: }
17: }
18: }
19: }
* @param
* @return
*/
ArrayList<ArrayList<String>> result = new ArrayList<ArrayList<String>>();
public ArrayList<ArrayList<String>> partition(String s) {
ArrayList<String> list = new ArrayList<String>();
findisPalindrome(s,0,list);
return result;
}
/**
* @param s
* @param i
* @param list
* @return
*/
private void findisPalindrome(String s, int start, ArrayList<String> list) {
if(start>= s.length()&& !list.isEmpty()){
result.add(new ArrayList(list));// 这个比较关键否则的话,如果是result.add(list)是不行的 必须New 一个
}else{
for(int end = start; end<=s.length(); end++){
String str = s.substring(start,end);
if(isPalindrome(str)){
list.add(str);
findisPalindrome(s,end,list);
list.remove(list.size()-1);//回退 进入下一个
}
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
String s = "aab";
ArrayList<ArrayList<String>> resultList = new LeetCodePalindromePartitioning().partition(s);
for(int i = 0; i< resultList.size(); i++){
System.out.println(resultList.get(i).toString());
}
}
/**
* 判断回文字符串
* @param str
* @return
*/
public boolean isPalindrome(String str) {
if(str.isEmpty()) return false;
boolean result = true;
for (int i = 0, j = str.length() - 1; i < j; i++, j--) {
if (str.charAt(i) != str.charAt(j)) {
result = false;
break;
}
}
return result;
}
}
相关文章推荐
- 每日进步之leetcode15题C++实现代码
- LeetCode 206. Reverse Linked List(迭代和递归两种实现)
- 二叉树系列 - [LeetCode] Symmetric Tree 判断二叉树是否对称,递归和非递归实现
- leetcode 二叉树遍历非递归实现
- 车厢调度(回溯法)递归实现
- leetcode | 二叉树的前序遍历、中序遍历、后续遍历的非递归实现
- leetcode 二叉树后续遍历的递归和非递归实现
- LeetCode 94. Binary Tree Inorder Traversal 树的前序、中序,后序遍历的非递归实现
- 每日AC--leetcode-- 2.Add Two Numbers
- leetcode 102.Binary Tree Level Order Traversal-数的层次遍历|递归|非递归实现
- 【启发】leetcode - 145. Binary Tree Postorder Traversal【二叉树 + 后序遍历 + 非递归实现】
- LeetCode Binary Tree Postorder Traversal(二叉树的后序遍历 非递归实现)
- 二叉树系列 - [LeetCode] Symmetric Tree 判断二叉树是否对称,递归和非递归实现
- 每日一题 LeetCode 有效的数字 Python实现
- 小型迷宫实现---迷宫算法(递归回溯法)
- Leetcode 226: Invert Binary Tree(二叉树反转 递归、非递归实现)
- leetcode解题之21# Merge Two Sorted Lists Java版 递归和非递归实现
- 用回溯法递归实现迷宫
- leetcode——Implement strStr() 实现字符串匹配函数(AC)
- 【LeetCode】 Invert Binary Tree(二叉树反转 递归、非递归实现)