标签:return leetcode nbsp tco ict val log stat length
public static int[] TwoSum(int[] nums, int target) { int[] result = new int[2]; //key为数值,value为它的索引值 Dictionary<int, int> temp = new Dictionary<int, int>(); for (int i = 0; i < nums.Length; i++) { int n; //判断nums[i]是否在字典里,不在加入 bool exist = temp.TryGetValue(nums[i], out n); if (!exist) { temp.Add(nums[i], i); } //判断target - nums[i]是否在字典里,在的话并且n与i不相等;返回 bool exist2 = temp.TryGetValue(target - nums[i], out n); if (exist2 && n < i) { result[0] = n; result[1] = i; return result; } } return result; }
标签:return leetcode nbsp tco ict val log stat length
原文地址:http://www.cnblogs.com/pengdotnet/p/Two-Sum.html