Created
August 13, 2022 06:41
-
-
Save letscodego/cabad03474d0c532e6ad981a1574fde0 to your computer and use it in GitHub Desktop.
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
public static int[] TwoSum(int[] nums, int target) | |
{ | |
if (nums.Length < 2) | |
return new int[2]; | |
var dic = new Dictionary(); | |
for (var i = 0; i < nums.Length; i++) | |
{ | |
if (dic.ContainsKey(target - nums[i])) | |
{ | |
return new int[] { (int)dic[target - nums[i]], i }; | |
} | |
else | |
{ | |
dic.Add(nums[i], i); | |
} | |
} | |
return new int[2]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment