-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path0170-TwoSumIIIDataStructureDesign.cs
57 lines (49 loc) · 1.54 KB
/
0170-TwoSumIIIDataStructureDesign.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
//-----------------------------------------------------------------------------
// Runtime: 216ms
// Memory Usage: 42.6 MB
// Link: https://leetcode.com/submissions/detail/406709543/
//-----------------------------------------------------------------------------
using System.Collections.Generic;
namespace LeetCode
{
public class _0170_TwoSumIIIDataStructureDesign
{
private readonly IDictionary<int, int> counts;
/** Initialize your data structure here. */
public _0170_TwoSumIIIDataStructureDesign()
{
counts = new Dictionary<int, int>();
}
/** Add the number to an internal data structure.. */
public void Add(int number)
{
if (counts.ContainsKey(number))
counts[number]++;
else
counts[number] = 1;
}
/** Find if there exists any pair of numbers which sum is equal to the value. */
public bool Find(int value)
{
foreach (var key in counts.Keys)
{
var num = value - key;
if (num != key)
{
if (counts.ContainsKey(num)) return true;
}
else
{
if (counts[key] > 1) return true;
}
}
return false;
}
}
/**
* Your TwoSum object will be instantiated and called as such:
* TwoSum obj = new TwoSum();
* obj.Add(number);
* bool param_2 = obj.Find(value);
*/
}