class Solution {
LinkedList<Integer> path = new LinkedList<>();
List<List<Integer>> res = new LinkedList<>();
public List<List<Integer>> combinationSum(int[] candidates, int target) {
backtrack(candidates, target, 0);
return res;
}
private void backtrack(int[] candidates, int target, int stIdx){
if(target < 0)return;
if(target == 0){
res.add(new LinkedList<>(path));
return;
}
for(int i = stIdx; i < candidates.length; i++){
path.add(candidates[i]);
backtrack(candidates, target - candidates[i], i);
path.removeLast();
}
}
}
class Solution {
LinkedList<Integer> path = new LinkedList<>();
List<List<Integer>> res = new LinkedList<>();
int trackSum = 0;
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
if(candidates.length == 0)return res;
Arrays.sort(candidates);
backtrack(candidates, 0, target);
return res;
}
private void backtrack(int[] candidates, int start, int target){
if(trackSum > target)return;
if(trackSum == target)res.add(new LinkedList<>(path));
for(int i = start; i < candidates.length; i++){
if(i > start && candidates[i] == candidates[i - 1]){
continue;
}
path.add(candidates[i]);
trackSum += candidates[i];
backtrack(candidates, i + 1, target);
path.removeLast();
trackSum -= candidates[i];
}
}
}
class Solution {
LinkedList<String> path = new LinkedList<>();
List<List<String>> res = new LinkedList<>();
public List<List<String>> partition(String s) {
backtrack(s, 0);
return res;
}
private void backtrack(String s, int start){
if(start == s.length()){
res.add(new LinkedList<String>(path));
}
for(int i = start; i < s.length(); i++){
if(!isPalindrome(s, start, i)){
continue;
}
path.addLast(s.substring(start, i + 1));
backtrack(s, i + 1);
path.removeLast();
}
}
private boolean isPalindrome(String s, int low, int high){
while(low < high){
if(s.charAt(low) != s.charAt(high)){
return false;
}
low++;
high--;
}
return true;
}
}