Skip to content

Instantly share code, notes, and snippets.

@linnet8989
Last active February 21, 2017 11:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save linnet8989/d615adea377d7b4c68278b93891459a2 to your computer and use it in GitHub Desktop.
Save linnet8989/d615adea377d7b4c68278b93891459a2 to your computer and use it in GitHub Desktop.
1. Two Sum
using System.Collections;
public class Solution
{
public int[] TwoSum(int[] nums, int target)
{
int pair;
Hashtable ht = new Hashtable();
for (int i = 0; i < nums.Length; i++)
{
if (ht.ContainsKey(nums[i]))
{
ht[nums[i]] = i;
}
else
{
ht.Add(nums[i], i);
}
}
for (int i = 0; i < nums.Length; i++)
{
pair = target - nums[i];
if (ht.ContainsKey(pair))
{
if ((int)ht[pair] != i)
{
return new int[] { i, (int)ht[pair] };
}
}
}
return new int[] { 0, 0 };
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment