using System;
using System.Collections.Generic;
using System.Collections;
class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param numbers int整型一维数组
* @param target int整型
* @return int整型一维数组
*/
public List<int> twoSum (List<int> numbers, int target) {
// write code here
Hashtable hash=new Hashtable();
for(int i=0;i<numbers.Count;i++)
{
var num=numbers[i];
if(hash.ContainsKey(target-num))
return new List<int>{ (int)hash[target-num]+1,i+1};
else{
if(!hash.ContainsKey(num))
hash.Add(num, i);
}
}
return null;
}
}
C# 用hash还不如2重循环?



京公网安备 11010502036488号