C# OC one time O(nlogn)


  • 0
    Y

    public class Solution {
    public IList<IList<int>> ThreeSum(int[] nums) {

        //sort array
        Array.Sort(nums);
        
        //need to be unique
        
        var resultInHash = new Dictionary<int,Dictionary<int,int>>();
        
        int low=0, high = nums.Length - 1;
        
        for(int i=0; i<nums.Length && nums[i]<=0; i++)
        {
            
            low=i+1; high=nums.Length - 1;
            
            while(low < high)
            {
                if(nums[low]+nums[high]+nums[i]==0)
                {
                    if(resultInHash.ContainsKey(nums[i]))
                    {
                        if(!resultInHash[nums[i]].ContainsKey(nums[low]))
                            resultInHash[nums[i]].Add(nums[low],nums[high]);
                    }
                    else
                    {
                        var son=new Dictionary<int,int>();
                        son.Add(nums[low],nums[high]);
                        resultInHash.Add(nums[i],son);
                    }
                    low+=1;
                    high-=1;
                }
                else if(nums[low]+nums[high]+nums[i]<0)
                {
                    low+=1;
                }
                else
                {
                    high-=1;
                }
            }
        }
        
        var result=new List<IList<int>>();
        
        foreach(var item in resultInHash)
        {
            foreach(var subItem in item.Value)
            {
                var sublist=new List<int>();
                  sublist.Add(item.Key);    
                  sublist.Add(subItem.Key);
                  sublist.Add(subItem.Value);
                  result.Add(sublist);
            }
        }
        
        return result;
    }
    

    }


Log in to reply
 

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