1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| public class Solution { public int[] TwoSum(int[] nums, int target) { var result = new int[2]; var dict = new Dictionary<int, int>();
for (int i = 0;i < nums.Length; i++) { var remain = target - nums[i]; if (dict.ContainsKey(remain)) { result[1] = i; result[0] = dict[remain];
return result; }
if (dict.ContainsKey(nums[i]) == false) { dict.Add(nums[i],i); } }
return null; } }
|