C# Version with comments


  • 0
    J
    using System.Linq;
    
    public class Solution {
        public IList<IList<int>> ThreeSum(int[] nums) {
            /* Contains the result. */
            Dictionary<int, IList<int>> result = new Dictionary<int, IList<int>>();
            
            /* Contains a map for negative and positive numbers. */
            Dictionary<int, int> positives = new Dictionary<int, int>();
            Dictionary<int, int> negatives = new Dictionary<int, int>();
            
            /* Returns empty result if invalid array is provided. */
            if (nums.Length < 3)
                return new List<IList<int>>();
            
            /* Maps negative and positive numbers, the value in the dictionary matches the number of ocurrences. */
            foreach(int num in nums.OrderBy( n => n))
            {
                Dictionary<int, int> dictionary = null;
                
                if (num >= 0)
                {
                    dictionary = positives;
                    
                    if (!dictionary.ContainsKey(num))
                        dictionary.Add(num, 1);
                    else
                        dictionary[num] = dictionary[num] + 1;
                }
                
                if (num < 0)
                {
                    dictionary = negatives;
                    
                    if (!dictionary.ContainsKey(num))
                        dictionary.Add(num, 1);
                    else
                        dictionary[num] = dictionary[num] + 1;
                }
            }
            
            /* Performs positive vs positive comparison, the other value must be in the negative dictionary. */
            foreach(int num1 in positives.Keys)
            {
                foreach(int num2 in positives.Keys)
                {
                    bool triplet = false;
                    
                    /* If num1 and num2 are the same, the occurrance in the dictionary for num1 must be equal or greater than 2. */
                    if (num1 == num2 && positives[num1] < 2) /* Ilegal iteration. */
                        continue;
                        
                    /* Accounts for special (0,0,0) case. */
                    if (num1 == 0 && num2 == 0 && positives[0] >= 3)
                        triplet = true;
                    
                    /* Negative contains the required negative value for the sum to equate to 0. */
                    if (negatives.ContainsKey((num1 + num2) * -1))
                        triplet = true;
                    
                    /* If it's a triplet add to the result. */
                    if (triplet)
                        AddToList(result, num1, num2, (num1 + num2) * -1);
                }
            }
            
            /* Performs negative vs negative comparison, the other value must be in the positive dictionary. */
            foreach(int num1 in negatives.Keys)
            {
                foreach(int num2 in negatives.Keys)
                {
                    bool triplet = false;
                    
                    /* If num1 and num2 are the same, the occurrance in the dictionary for num1 must be equal or greater than 2. */
                    if (num1 == num2 && negatives[num1] < 2) /* Ilegal iteration. */
                        continue;
                        
                    /* Positive contains the required positive value for the sum to equate to 0. */
                    if (positives.ContainsKey((num1 + num2) * -1))
                        triplet = true;
                    
                    /* If it's a triplet add to the result. */
                    if (triplet)
                        AddToList(result, num1, num2, (num1 + num2) * -1);
                }
            }
            
            /* Returns the result. */
            return new List<IList<int>>( result.Values );
        }
        
        /* Adds the value to the dictionary. */
        public void AddToList(Dictionary<int, IList<int>> result, int num1, int num2, int num3)
        {
            /* Sorts numbers in the result. */
            int[] nums = new int[]{ num1, num2, num3 }.OrderBy( n => n ).ToArray();
            
            /* Gets unique hashcode. */
            int key = (nums[1].ToString() + nums[0].ToString() + nums[1].ToString()).GetHashCode();
            
            /* saves the result using the recently generated hashcode to ensure uniqueness. */
            result[key] = 
                new List<int>(
                    nums
                );
        }
    }

Log in to reply
 

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