Created
March 1, 2022 13:02
-
-
Save andybrackley/421d420c1048e8756683f77c25229f45 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
// https://leetcode.com/problems/two-sum/submissions/ | |
public class Solution { | |
public int[] TwoSum(int[] nums, int target) { | |
var dict = new Dictionary<int, (int num, int index)> (); | |
for(int i = 0; i < nums.Length; ++i) { | |
var current = nums[i]; | |
var required = target - current; | |
if(dict.TryGetValue(required, out var avail)) { | |
return new[] { avail.index, i }; | |
} | |
dict[current] = (current, i); | |
} | |
return new int[0]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment