-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path15_3Sum.cs
123 lines (97 loc) · 3.67 KB
/
15_3Sum.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _16_3Sum
{
class Program
{
static void Main(string[] args)
{
// test 3 sum closest
int[] arr = new int[6] {-1, 0, 1, 2, -1, -4};
IList<IList<int>> triplets = threeSumClose(arr);
}
/*
* May 17, 2016
* Work on this 3 sum close algorithm
*
* Given an array S of n integers, are there elements a, b, c in S
* such that a + b + c = 0? Find all unique triplets in the array
* which gives the sum of zero.
Note:
Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c)
The solution set must not contain duplicate triplets.
*
For example, given array S = {-1 0 1 2 -1 -4},
A solution set is:
(-1, 0, 1)
(-1, -1, 2)
*
* C# Array - get sum of array element
* http://stackoverflow.com/questions/2419343/how-to-sum-up-an-array-of-integers-in-c-sharp
*/
public static IList<IList<int>> threeSumClose(int[] nums)
{
int target = 0;
IList<IList<int>> triplets = new List<IList<int>>();
HashSet<string> keys = new HashSet<string>(); // -1, 0, 1 -> key string" "-1,0,1,"
if (nums == null || nums.Length == 0)
return triplets;
Array.Sort(nums);
int len = nums.Length;
for (int i = 0; i < len - 2; i++) // len = 3, test case passes!
{
int[] trialTriplet = new int[3];
trialTriplet[0] = nums[i];
// call two sum function
int newTarget = target - trialTriplet[0];
int head = i + 1;
int tail = len - 1;
while (head < tail)
{
trialTriplet[1] = nums[head];
trialTriplet[2] = nums[tail];
int twoSumValue = trialTriplet[1] + trialTriplet[2];
if (twoSumValue == newTarget) // newTarget, not target
{
// check if the key is in key hashset or not,
// if yes, then skip it; otherwise, add it to result
string key = getKey(trialTriplet, 3);
if (!keys.Contains(key))
{
keys.Add(key);
IList<int> triplet = new List<int>();
for (int j = 0; j < 3; j++)
triplet.Add(trialTriplet[j]);
triplets.Add(triplet);
}
// continue to search
head++;
tail--;
}
else if (twoSumValue > newTarget)
{
tail--;
}
else if (twoSumValue < newTarget)
{
head++;
}
}
}
return triplets;
}
private static string getKey(int[] arr, int len)
{
string key = "";
for (int j = 0; j < 3; j++)
{
key += arr[j].ToString();
key += ",";
}
return key;
}
}
}