Skip to content

Instantly share code, notes, and snippets.

@andybrackley
Created March 1, 2022 13:02
Show Gist options
  • Save andybrackley/421d420c1048e8756683f77c25229f45 to your computer and use it in GitHub Desktop.
Save andybrackley/421d420c1048e8756683f77c25229f45 to your computer and use it in GitHub Desktop.
// 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