Last active
February 21, 2017 11:56
-
-
Save linnet8989/d615adea377d7b4c68278b93891459a2 to your computer and use it in GitHub Desktop.
1. Two Sum
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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