public class Solution {
public int[] TwoSum(int[] numbers, int target) {
Dictionary<int, int> dic = new Dictionary<int, int>();
int count = numbers.Count();
List<KeyValuePair<int, int>> list = new List<KeyValuePair<int, int>>(); for (int i = ; i < count; i++)
{
if (!dic.ContainsKey(numbers[i]))
{
dic.Add(numbers[i], i);
list.Add(new KeyValuePair<int, int>(numbers[i], i));
}
} int[] ary = new int[]; for (int i = ; i < list.Count; i++)
{
for (int j = i; j < list.Count; j++)
{
var d1 = list[i];
var d2 = list[j];
if (d1.Key + d1.Key == target && d2.Value - d1.Value > )
{
ary[] = d1.Value + ;
ary[] = d1.Value + ;
return ary;
}
else if (i != j && d1.Key + d2.Key == target)
{
ary[] = d1.Value + ;
ary[] = d2.Value + ;
return ary;
}
}
} return ary;
}
}

https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/#/description

补充一个“双指针”思想的解决方案,使用python实现:

 class Solution:
def twoSum(self, numbers: 'List[int]', target: 'int') -> 'List[int]':
i=0
j=len(numbers)-1
while numbers[i] + numbers[j] != target:
if numbers[i] + numbers[j]>target:
j-=1
else:
i+=1 return [i+1,j+1]
05-11 20:41