-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1 Two Sum.cs
31 lines (27 loc) · 962 Bytes
/
1 Two Sum.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
public class Solution {
public int[] TwoSum(int[] nums, int target) {
//A Map to store value and their index
Dictionary<int, int> numToIndex = new Dictionary<int, int>();
//Enumerate through the list.
for (int i = 0; i < nums.Length; i++) {
//Check if difference between target and current value exist in map.
if (numToIndex.ContainsKey(target - nums[i])) {
//If yes, return its index and current index.
return new int[] {numToIndex[target - nums[i]], i};
}
//Else add value and its index.
numToIndex.TryAdd(nums[i], i);
}
//Else return blank list, since no elements add up to target.
return new int[] {};
}
}
class Program
{
static void Main(string[] args)
{
Solution sol = new Solution();
int[] nums = new int[] {2,5,6};
Console.WriteLine(sol.TwoSum(nums, 8));
}
}