Skip to content

Commit

Permalink
LEETCODE-1: Two Sum
Browse files Browse the repository at this point in the history
  • Loading branch information
RomaLikhachev committed Feb 14, 2024
1 parent 85a24ef commit 02fb269
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CSharpEducation.sln
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Task1", "Task1\Task1.csproj", "{D0083D4D-28D6-438D-A4BD-04917D0FC77F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LeetCode1", "LeetCode1\LeetCode1.csproj", "{38E465BC-9B06-4120-8060-02C10053AF7D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -12,5 +14,9 @@ Global
{D0083D4D-28D6-438D-A4BD-04917D0FC77F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D0083D4D-28D6-438D-A4BD-04917D0FC77F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D0083D4D-28D6-438D-A4BD-04917D0FC77F}.Release|Any CPU.Build.0 = Release|Any CPU
{38E465BC-9B06-4120-8060-02C10053AF7D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{38E465BC-9B06-4120-8060-02C10053AF7D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{38E465BC-9B06-4120-8060-02C10053AF7D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{38E465BC-9B06-4120-8060-02C10053AF7D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
10 changes: 10 additions & 0 deletions LeetCode1/LeetCode1.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
35 changes: 35 additions & 0 deletions LeetCode1/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// https://leetcode.com/problems/two-sum/

using System.Collections;

int[] result = new Solution().TwoSum([1, 1, 7, 1, 1, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], 11);
Array.ForEach(result, Console.WriteLine);

public class Solution
{
public int[] TwoSum(int[] nums, int target)
{
var result = new int[2];
var verifiedNumDict = new Dictionary<int, int>(nums.Length);

for (var index = 0; index < nums.Length; index++)
{
var value = nums[index];

var remain = target - value;

if (verifiedNumDict.ContainsKey(remain))
{
result[0] = index;
result[1] = verifiedNumDict[remain];
break;
}
else
{
verifiedNumDict.TryAdd(value, index);
}
}

return result;
}
}

0 comments on commit 02fb269

Please sign in to comment.