这个是C#版本
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TwoSum
{
class Program
{
static void Main(string[] args)
{
int[] numbers = new int[] { 1, 2, 10, 11, 3, 7, 11, 15 };
int[] li = twoSum(numbers, 9);
if (li != null && li.Length > 1)
{
Console.WriteLine("index1 = {0}, index2 = {1}", li[0], li[1]
);
}
}
static int[] twoSum(int[] numbers, int target)
{
Dictionary dict = new Dictionary();
for (int i = 0; i < numbers.Length; i++)
{
int rem = target - numbers[i];
if (dict.ContainsKey(rem))
{
return new int[2] { dict[rem], i+1 };
}
else
{
dict[numbers[i]] = i+1;
}
}
return null;
}
}
}