-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTwoSum.cs
48 lines (43 loc) · 1.57 KB
/
TwoSum.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
//, return indices of the two numbers such that they add up to target.
public class Solution
{
//Write an array of integers nums and an integer target
public int[] TwoSum(int[] nums, int target)
{
//if the array is null or empty, return null
if(nums == null || nums.Length == 0)
{
return null;
}
//use a dictionary to store the element and its index
Dictionary<int, int> dict = new Dictionary<int, int>();
//use a for loop to traverse the array
for(int i = 0; i < nums.Length; i++)
{
//if the dictionary contains the key of the target minus the element
if(dict.ContainsKey(target - nums[i]))
{
//return the index of the element and the index of the target minus the element
return new int[] {dict[target - nums[i]], i};
}
//if the dictionary does not contain the key of the target minus the element
else
{
//if the dictionary contains the key of the element
if(dict.ContainsKey(nums[i]))
{
//continue
continue;
}
//if the dictionary does not contain the key of the element
else
{
//add the element and its index to the dictionary
dict.Add(nums[i], i);
}
}
}
//return null
return null;
}
}