-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
85a24ef
commit 02fb269
Showing
3 changed files
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |