public static IList <IList <int> > ThreeSum(int[] nums) { if (nums.Length == 0) { return(new List <IList <int> >()); } Dictionary <int, int> num_count = new Dictionary <int, int>(); //build dictionary foreach (int n in nums) { if (num_count.ContainsKey(n)) { num_count[n]++; } else { num_count.Add(n, 1); } } Array.Sort(nums); int seekVal; int counter = 0; Dictionary <String, threeTuple> result = new Dictionary <string, threeTuple>(); threeTuple buff; // give each 2x2 triangle combination for (int i = 0; i < nums.Length; i++) { for (int j = nums.Length - 1; j > i; j--) { seekVal = -(nums[j] + nums[i]); Console.WriteLine("i:" + i + "|j:" + j); Console.WriteLine("Num i:" + nums[i] + "|Num j:" + nums[j] + "|seek:" + seekVal); if (seekVal < nums[i]) { continue; } /* if seek value is out of the array's range * TEST FOR MORE OPTIMIZATION */ if (nums[nums.Length - 1] < seekVal) { j = i; } //sortedNums[0] > seekVal || if (num_count.ContainsKey(seekVal)) { counter = num_count[seekVal]; if (nums[i] == seekVal) { counter--; } if (nums[j] == seekVal) { counter--; } if (counter < 1) { continue; } buff = new threeTuple(nums[i], nums[j], seekVal); if (!result.ContainsKey(buff.ToString())) { result.Add(buff.ToString(), buff); } } } } IList <IList <int> > fOut = new List <IList <int> >(); foreach (threeTuple t in result.Values) { fOut.Add(t.toList()); } return(fOut); }
public static IList <IList <int> > ThreeSum_2(int[] nums) { // if (nums.Length == 0) { return(new List <IList <int> >()); } Dictionary <int, int> num_count = new Dictionary <int, int>(); //build dictionary foreach (int n in nums) { if (num_count.ContainsKey(n)) { num_count[n]++; } else { num_count.Add(n, 1); } } int[] sortedNums = new int[num_count.Keys.Count]; num_count.Keys.CopyTo(sortedNums, 0); Array.Sort(sortedNums); int seekVal; Dictionary <String, threeTuple> result = new Dictionary <string, threeTuple>(); threeTuple buff; // give each 2x2 triangle combination for (int i = 0; i < sortedNums.Length; i++) { for (int j = sortedNums.Length - 1; j > i; j--) { seekVal = -(sortedNums[j] + sortedNums[i]); Console.WriteLine("i:" + i + "|j:" + j); Console.WriteLine("Num i:" + sortedNums[i] + "|Num j:" + sortedNums[j] + "|seek:" + seekVal); /* if seek value is out of the array's range * TEST FOR MORE OPTIMIZATION */ if (sortedNums[sortedNums.Length - 1] < seekVal) { j = i; //sortedNums[0] > seekVal || } // if seek value is a duplciate. will be saved for straight pass of sortedNums if (seekVal == sortedNums[i] || seekVal == sortedNums[j]) { continue; } if (num_count.ContainsKey(seekVal)) { buff = new threeTuple(sortedNums[i], sortedNums[j], seekVal); if (!result.ContainsKey(buff.ToString())) { result.Add(buff.ToString(), buff); } } } } foreach (int i in sortedNums) { if (num_count[i] < 2) { continue; } seekVal = -2 * i; num_count[i] -= 2; if (num_count.ContainsKey(seekVal) && num_count[seekVal] > 0) { buff = new threeTuple(i, i, seekVal); if (!result.ContainsKey(buff.ToString())) { result.Add(buff.ToString(), buff); } } num_count[i] += 2; } IList <IList <int> > fOut = new List <IList <int> >(); foreach (threeTuple t in result.Values) { fOut.Add(t.toList()); } return(fOut); }