Easy to understand Java backtrack recursion solution


#1
public class Solution {
    public ArrayList<ArrayList<Integer>> combinationSum(ArrayList<Integer> A, int B) {
        ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
        if(A == null || A.isEmpty()) return result;
        ArrayList<Integer> inList = new ArrayList<>();
        int startIndex = 0;
        Collections.sort(A);
        combinationSum(result, A, B, inList, startIndex);
        return result;
    }
    public void combinationSum(ArrayList<ArrayList<Integer>> result, ArrayList<Integer> A, int target, ArrayList<Integer> inList, int start) {
        if(target == 0) {
            result.add(new ArrayList(inList));
            return;
        }
        for(int i = start; i < A.size(); i++) {
            int newTarget = target - A.get(i);
            if(newTarget < 0) {
                break;
            }
            if(i != 0 && A.get(i) == A.get(i - 1)) {
                continue;
            }
            inList.add(A.get(i));
            combinationSum(result, A, newTarget, inList, i);
            inList.remove(inList.size() - 1);
        }
    }
}