Why my java code TLE?


  • 0
    W

    public class Solution {

    public List<List<Integer>> threeSum(int[] nums) {
        Arrays.sort(nums);
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        for(int i=0; i<nums.length-2; i++){
            for(int j = i+1; j<nums.length-1; j++)
                if(Arrays.binarySearch(nums, j, nums.length, -nums[i]-nums[j])>=0)
                	if(!result.contains(Arrays.asList(nums[i], nums[j], -nums[i]-nums[j])))
                    result.add(new ArrayList<Integer> (Arrays.asList(nums[i], nums[j], -nums[i]-nums[j])));
        }
        return result;
    }
    

    }

    O(nnlgn)


  • 0
    R

    Unfortunately O(n2log(n)) is too slow. See this post for a faster O(n2) solution.


Log in to reply
 

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