generated from eyamenko/dotnet-template-repository
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem1.cs
39 lines (34 loc) · 1.2 KB
/
Problem1.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
namespace LeetCode;
/// <summary>
/// <see href="https://leetcode.com/problems/two-sum/">Two Sum</see>.
/// </summary>
public static class Problem1
{
/// <summary>
/// Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
/// You may assume that each input would have exactly one solution, and you may not use the same element twice.
/// You can return the answer in any order.
/// Time complexity: O(n).
/// Space complexity: O(n).
/// </summary>
/// <param name="nums">Array to traverse.</param>
/// <param name="target">Target sum.</param>
/// <returns>Indices of the two numbers that add up to the target.</returns>
public static int[] TwoSum(int[] nums, int target)
{
var dict = new Dictionary<int, int>(nums.Length);
for (var i = 0; i < nums.Length; i++)
{
var diff = target - nums[i];
if (dict.ContainsKey(diff))
{
return new[] { dict[diff], i };
}
if (!dict.ContainsKey(nums[i]))
{
dict.Add(nums[i], i);
}
}
return Array.Empty<int>();
}
}