一般的回溯算法问题
public class Solution { public List<List<String>> partition(String s) { List<List<String>> allResults = new ArrayList<List<String>>(); if(s.length() == 1){ ArrayList<String> result = new ArrayList<String>(); result.add(s.substring(0, 1)); allResults.add(result); return allResults; } for(int i=1; i<s.length()+1; i++){ if(isPalindrome(s.substring(0, i))){ ArrayList<String> result = new ArrayList<String>(); result.add(s.substring(0, i)); trackBack(s, i-1, result, allResults); } } return allResults; } private void trackBack(String s, int end, ArrayList<String> result, List<List<String>> allResults){ if(end == s.length()-1){ ArrayList<String> a =(ArrayList<String>)result.clone(); allResults.add(a); return; } for(int i=end+2; i<=s.length(); i++){ String subS = s.substring(end+1, i); if(isPalindrome(subS)){ int size = result.size(); result.add(size, subS); trackBack(s, i-1, result, allResults); result.remove(size); } } } private boolean isPalindrome(String s){ String rs = new StringBuffer(s).reverse().toString(); if(s.equals(rs)){ return true; } return false; } }
相关推荐
leetcode思维导图-回溯
java lru leetcode leetcode-algorithms-java leetcode 算法笔记-java
java lru leetcode LeetCode-Tag-Java 解决方案 LeetCode 的解决方案 指数
力扣解题java版本
https://github.com/geekxingyun/leetcode-java-algorithm-solution.git 然后你会发现当前项目有两个模块。 完成:它是完成的项目,并有一些关于 leetcode 的答案。 初始:只需在模块项目中编写代码 如果您不知道...
leetcode 答案解析 golang解答
leetcode Leetcode-Java-解决方案 Java 中 Leetcode 问题的解决方案
用Java实现基础数据结构,排序算法、经典算法以及leetcode刷题记录_Java_下载.zip
LeetCode-Python-Java zhouzhenping/LeetCode-Python&Java 参考代码来源 力扣(LeetCode) 链接: 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 Github 01 两数之和 给定一个整数数组 ...
#Leetcode-In-Java 代码并不全是本人写的,有的参考了网络上其他前辈的想法,但都能在OJ上AC。 ###索引 1 . Two-Sum 要点: - 利用java中Array对象的sort方法排序,使得整个数组呈升序状态 - 再利用两段取点...
leetcode-helper-1.7.1
leetcode 答案LeetCode-答案-Java 我的 leetcode 用伪代码和分析用 java 回答
分类LeetCode-Java-接受 这是 Leetcode 问题的 Java 解决方案。 细节 标题和答案格式 /* * 17. Letter Combinations of a Phone Number * Target: Given a string containing digits from 2-9 inclusive, return all...
leetcode-tag-dynamic programming
leetcode-tag-Stack
然后通过用循环来解:假设第一个for循环是一个数组的循环,而后它的内嵌循环是也是这个数组,只是下标从0变成了1,这样,在第一次循环时,第1个元素会与其他所有元素
leetcode-tag-array
LeetCode-Practice-Java leetcode 练习题 这个项目就是自己练习leetCode的实际Demo 有些公司就会问一些算法问题,即便是不问的话自己不学这块感觉也对不起自己的职业。。。 所以自己在学习的时候 也打算把优秀的答案...
leetcode-for-Java Record the process of leetcode for Java. 引言 记录一下,在leetcode上刷题的过程。 前期以刷题为主。有空的话就更新一下这个目录,然后顺带把有写问题的解题思路写上。 目录
LeetCode题解 - Java语言实现-181页.pdf