How can I get the 3sum leetcode Java solution?

314    Asked by Amitraj in Java , Asked on Oct 12, 2022

 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.

Answered by Andrew Jenkins

This is a nice start, but there's some stylistic stuff that could be improved for the 3sum leetcode java.


Generally it's a good idea to name variables for what their purpose is. Names like l1 are difficult to understand. This varies depending on the language, but in Java people tend to be explicit with their variable naming, preferring descriptive names over concise ones. When declaring a data structure, convention in Java is to declare the type as the interface.

  public boolean areDuplicates(ArrayList first, ArrayList second)

is more commonly written as

  public boolean areDuplicates(List first, List second)

This is because List defines a set of behaviours that you want to have, and ArrayList defines how they are implemented. If someone has made a data structure with the exact same behaviour as an ArrayList, then you want your methods to support that data structure as well. The format of the code is quite unusual for Java. I'd run your code through an auto formatter, which deals with all that stuff for you. Personally I use Eclipse's default one, but there's loads of choice out there. This isn't a complete review, and it's mainly my opinion, so feel free to include or discard this advice.



Your Answer

Interviews

Parent Categories