Personal Blog
  • 💻Notes for Computer Science
  • Leetcode
    • Array
      • Container with most water
      • 3Sum
      • Next Permutation
      • Valid Sudoku
      • Permutation II
      • Combination Sum
      • Triangle
      • Maximal Square
      • Pairs of Songs with Total Duration Divisible by 60
      • Numbers At Most N Given Digit Set
      • Possible Sum
      • Swap Lex Order
      • Partition Equal Subset Sum
      • Domino and Tromino
      • Numbers At Most N Given Digits
      • Car Pooling
      • Surrounding Regions
      • Min Size Subarray Sum
      • Burst Balloons
      • Jump Game I
      • Jump Game II
      • House Robber II
      • Delete and Earn
      • Word Break
      • Decode Ways
      • Longest Increasing Subsequence
      • Cherry Pickup
      • Rotate Image
    • LinkedList
      • IsListPalindrome
      • Linked List Cycle
      • MergeTwoLinkedList
      • ReverseNodeInKGroup
      • RearrangeLastN
      • Remove Duplicates From Sorted List
      • RemoveKFromList
    • String
      • Generate Parentheses
      • Longest Valid Parentheses
      • Longest Common Subsequence
      • Count and Say
      • Decode String
      • Permutation in String
    • Tree
      • House Robber III
      • Convert Sorted Array to Binary Search Tree
      • Restore Binary Tree
      • Populating Next Right Pointers in Each Node II
      • Subtree of Another Tree
    • Graph
      • All Paths from Source to Target
      • Reorder Routes to Make All Paths Lead to the City Zero
      • Max Points on a Line
  • DBMS
    • DBMS Notes
  • Web App
    • Web Design
    • JavaScript
    • React.js
    • ReactNative
    • Mobile Design
    • Dialogue Flow
  • AnaplanIntern
    • Splunk
    • Docker
    • Kubernetes
  • 💰 Notes for Finance Concept
  • Analysis Concept
    • Volume Spread Analysis
    • Smart Money Concepts
Powered by GitBook
On this page
  1. Leetcode
  2. Array

3Sum

ID: 15

Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.

Input: nums = [-1,0,1,2,-1,-4]
Output: [[-1,-1,2],[-1,0,1]]

Idea

For loop + two pointer

Sort the given list for speed up

Iterate from the beginning of nums[] from 0-->length-1, set left = i & right = length-1

check sum of i, left, right-->left++ if smaller than 0; right++ if bigger than 0; add to result if ==0

public List<List<Integer>> threeSum(int[] nums) {
        List<Integer> numsList = new ArrayList<>();
        for(int i: nums){
            numsList.add(i);
        }
        Collections.sort(numsList);
        List<List<Integer>> result = new ArrayList<>();

        for(int i = 0; i<numsList.size(); i++){
            int left = i+1;
            int right = numsList.size()-1;
            while(left<right){
                if(numsList.get(i)+numsList.get(left)+numsList.get(right)<0){
                    left++;
                    continue;
                }
                else if(numsList.get(i)+numsList.get(left)+numsList.get(right)>0){
                    right--;
                    continue;
                }
                else if(numsList.get(i)+numsList.get(left)+numsList.get(right)==0){
                    List<Integer> curr = new ArrayList<>();
                    curr.add(numsList.get(i));
                    curr.add(numsList.get(left));
                    curr.add(numsList.get(right));
                    
                    if(!result.contains(curr)){
                         result.add(curr);
                    }
                    left++;
                }
                
            }
        }
        
        
        
        return result;
    }
PreviousContainer with most waterNextNext Permutation

Last updated 3 years ago