Skip to content

Commit 94bcc4f

Browse files
committed
add solution for Two Sum task
1 parent 02e3b05 commit 94bcc4f

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

C#/LeetCodeResolves/LeetCodeResolves/LeetCodeResolves.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
55
<TargetFramework>netcoreapp3.1</TargetFramework>
6-
<StartupObject>LeetCodeResolves.LongestPalindrome.Program</StartupObject>
6+
<StartupObject>LeetCodeResolves.TwoSum.Program</StartupObject>
77
</PropertyGroup>
88

99
</Project>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace LeetCodeResolves.TwoSum
6+
{
7+
class Program
8+
{
9+
static void Main(string[] args)
10+
{
11+
var nums = new int[] { -3, 4, 3, 90};
12+
var target = 0;
13+
var result = TwoSum(nums, target);
14+
15+
Console.WriteLine($"The result is: {result[0]}, {result[1]}.");
16+
Console.ReadLine();
17+
}
18+
19+
static int[] TwoSum(int[] nums, int target)
20+
{
21+
var dictionary = new Dictionary<int, int>();
22+
for(var i = 0; i < nums.Length; i++)
23+
{
24+
var complement = target - nums[i];
25+
if (dictionary.ContainsKey(complement))
26+
{
27+
return new int[] { i, dictionary[complement] };
28+
}
29+
30+
dictionary.Add(nums[i], i);
31+
}
32+
33+
throw new InvalidOperationException();
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)