Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3Sum.js #10

Closed
ralic opened this issue Sep 29, 2017 · 1 comment
Closed

3Sum.js #10

ralic opened this issue Sep 29, 2017 · 1 comment

Comments

@ralic
Copy link

ralic commented Sep 29, 2017

The javascript version is exceeding time limit for longer list.
Perhaps you may refer to following java code to develop a javascript version.

    public List<List<Integer>> threeSum(int[] nums) {
        
        List<Integer> list;
        List<List<Integer>> retList = new  LinkedList<>();
        Arrays.sort(nums);
        for( int i = 0; i < nums.length && nums[i] <= 0; i++){
            if( i > 0 && nums[i] == nums[i-1])
                continue;
            int sum = (-1)*nums[i], front = i + 1, behin = nums.length - 1;
            while( front < behin )
                if( nums[front] + nums[behin] > sum) 
                    behin--;
                else if( nums[front] + nums[behin] < sum) 
                    front++;
                else{
                    list = new LinkedList<Integer>();
                    list.add(nums[i]);
                    list.add(nums[front]);
                    list.add(nums[behin]);
                    retList.add(list);
                    while( front<behin && nums[front]==nums[front+1]) front++;
                    while( front<behin && nums[behin]==nums[behin-1]) behin--;
                    front++;
                    behin--;
                }
        }
        return retList;
    }
@lessfish
Copy link
Owner

lessfish commented Oct 8, 2017

@lessfish lessfish closed this as completed Oct 8, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants