Skip to content

Instantly share code, notes, and snippets.

@letscodego
Created August 13, 2022 06:41
Show Gist options
  • Save letscodego/cabad03474d0c532e6ad981a1574fde0 to your computer and use it in GitHub Desktop.
Save letscodego/cabad03474d0c532e6ad981a1574fde0 to your computer and use it in GitHub Desktop.
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