-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
35 lines (28 loc) · 872 Bytes
/
Program.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
// https://leetcode.com/problems/two-sum/
using System.Collections;
int[] result = new Solution().TwoSum([1, 1, 7, 1, 1, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], 11);
Array.ForEach(result, Console.WriteLine);
public class Solution
{
public int[] TwoSum(int[] nums, int target)
{
var result = new int[2];
var verifiedNumDict = new Dictionary<int, int>(nums.Length);
for (var index = 0; index < nums.Length; index++)
{
var value = nums[index];
var remain = target - value;
if (verifiedNumDict.ContainsKey(remain))
{
result[0] = index;
result[1] = verifiedNumDict[remain];
break;
}
else
{
verifiedNumDict.TryAdd(value, index);
}
}
return result;
}
}