HELP! My backtracking method, but it gets an LTE problem. Please help to correct me. Thanks a lot.


  • 0
    C

    My backtracking method, but it gets an LTE problem. Is this a way to solve this problem? I use some small cases, it can get a correct answer, but finally get an LTE probelm.
    Could you please help to correct me. Thanks a lot!

    By the way, could you please give me some hints on the time and space for this method.

    public int threeSumClosest(int[] nums, int target) {
        int[] res={Integer.MAX_VALUE};
        helper(nums,target,0,0,0,res);
        return res[0];
    }
    public void helper(int[] nums, int target,int index, int step, int cur, int[] res){
        if(step == 3){
            if(Math.abs(cur-target)<Math.abs(res[0]-target)){
                res[0]=cur;
            }
            return;
        }
        for(int i=index;i<nums.length;i++){
            helper(nums,target,i+1,step+1,cur+nums[i],res);
        }
    }

Log in to reply
 

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