Java solution only beat 30%...


  • 0
    L

    How to improve it?

    public class Solution {
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<List<Integer>> res = new ArrayList<>();
        judge(new ArrayList<Integer>(), 0, candidates, target, res, 0);
        return res;
    }
    
    private void judge(List<Integer> tempList, Integer tempSum, int[] candidates, int target,
                       List<List<Integer>> res, int curI) {
        for (int i = curI; i < candidates.length; i++) {
            int curTempSum = tempSum + candidates[i];
            if (curTempSum > target)
                continue;
            List<Integer> nextTempList = new ArrayList<>(tempList);
            nextTempList.add(candidates[i]);
            if (curTempSum == target)
                res.add(nextTempList);
            else
                judge(nextTempList, curTempSum, candidates, target, res, i);
        }
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.