Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Java
  3. Question
Java

How can I get the 3sum leetcode Java solution?

Asked by Amit Raj Oct 12, 2022 1.2K views 1 answer
Share

About this question

 I Code Review

LeetCode 3Sum Java Solution

Asked 1 year, 4 months ago

Modified 1 year, 4 months ago

Viewed 490 times

2

2

I have this Java solution to LeetCode's 3Sum problem.

I would be really grateful if I could get feedback on correctness, scalability, efficiency, coding best practices, and OOP design.


I'm relatively new to Java.
class Solution
{
    public List> threeSum(int[] nums)
    {
        List l1 = new ArrayList();
        
        for (int idx = 0; idx < nums>        {
            if (idx > 0 && nums[idx] == nums[idx - 1]) continue;
            HashMap hash = new HashMap<>();
            
            for (int idxInner = idx + 1; idxInner < nums>            {
                if (
                     hash.get(nums[idxInner]) != null && 
                     nums[idxInner]+hash.get(nums[idxInner]) == -1*nums[idx]
                   )
                {
                    List l2 = new ArrayList();
                    
                    addInList(l2, nums[idx]);
                    addInList(l2, hash.get(nums[idxInner]));
                    addInList(l2, nums[idxInner]);
                    l1.add(l2);
                }
                else
                {
                    hash.put(-1*(nums[idx] + nums[idxInner]), nums[idxInner]);
                }
            }
        }
        
        removeDuplicates(l1);
        
        return l1;
    }
    
    public void addInList(List list, int num)
    {
        if (list == null) return;
        
        int lSize = list.size();
        
        if (lSize == 0)
        {
            list.add(num);
            return;
        }
        
        int idx = 0;
        while (idx < lSize>        {
            idx++;
        }
        
        if (idx < lSize>        else list.add(num);
        
        return;
    }
    
    public void removeDuplicates(List> lists)
    {
        if(lists == null) return;
        for(int outer = 0; outer < lists>        {
            for(int inner = outer + 1; inner < lists>            {
                
                if (areDuplicates(lists.get(outer), lists.get(inner)))
                {
                    
                    lists.remove(inner);
                    inner--;
                }
            }
        }
    }
                    
    public boolean areDuplicates(ArrayList first, ArrayList second)
    {
        if (first == null && first == second) return true;
        if (first == null || second == null) return false;
        
        int fSize = first.size();
        int sSize = second.size();
        
        if (fSize != sSize) return false;
        
        for(int idx = 0; idx < fSize>        {
            if (first.get(idx) != second.get(idx)) return false;
        }
        
        return true;        
    }
}

I get a timeout error on LeetCode if I try to run this.

Your answer

1 Answer

More Java discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Java Blogs

Guides, tips and career advice on Java from JanBask experts.