Time limit excceed? how to solve the problem?


  • 0
    X

    {public class Solution {
    public List<List<Integer>> combinationSum2(int[] num, int target) {
    List<List<Integer>> lResult = combinationSum2(num, target, 0);
    if (lResult.size() > 0){
    for(List<Integer> lList : lResult){
    Collections.sort(lList);
    }
    }
    return lResult;
    }

    public List<List<Integer>> combinationSum2(int[] num, int target, int start){
        List<List<Integer>> lResult = new LinkedList<List<Integer>>();
        for (int i = start; i < num.length; i++){
            if (num[i] == target){
                List<Integer> lTmp = new LinkedList<Integer>();
                lTmp.add(num[i]);
                lResult.add(lTmp);
            } else if (num[i] < target){
                List<List<Integer>> lTmp = combinationSum2(num, target - num[i], start + 1);
                if (lTmp.size() > 0){
                    for (List<Integer> lList : lTmp){
                        lList.add(num[i]);
                        lResult.add(lList);
                    }
                }
            }
        }
        return lResult;
    }
    

    }
    }


Log in to reply
 

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