Answer via C# using HashTable:
public class Solution {
public int[] TwoSum(int[] nums, int target) {
List<int> arr = new List<int>();
Hashtable hashTable = new Hashtable();
for (int i = 0; i < nums.Length;i++)
{
hashTable[nums[i]] = i;
}
for (int i = 0; i < nums.Length; i++)
{
// get key2, index2
var key2 = target - nums[i];
if (hashTable.Contains(key2) && ((Int32)hashTable[key2] != i) && ((Int32)hashTable[key2] > i))
{
int i2 = (Int32)hashTable[key2];
arr.Add(i + 1);
arr.Add(i2 + 1);
}
}
return arr.ToArray();
}
}
No comments:
Post a Comment