-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetcode-1-Two_Sum.cs
47 lines (40 loc) · 1.05 KB
/
leetcode-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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using System;
using System.Collections.Generic;
public class Solution {
public int[] TwoSum(int[] nums, int target) {
Dictionary<int, int> vals_inds = new Dictionary<int, int>();
int[] res = new int[2];
for(int i = 0; i<nums.Length; ++i)
{
int diff = (target-nums[i]);
if(vals_inds.ContainsKey(diff))
{
res[0] = i;
res[1] = vals_inds[diff];
return res;
}
vals_inds[nums[i]] = i;
}
return res;
}
}
public class Program
{
public static void Main(string[] args)
{
int[] nums = {2,7,11,15};
int target = 9;
Solution solution = new Solution();
int[] res = solution.TwoSum(nums, target);
Console.Write("[");
for(int i = 0; i<res.Length; ++i)
{
Console.Write(res[i]);
if(i<res.Length-1)
{
Console.Write(", ");
}
}
Console.Write("]");
}
}