Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
import java.util.Hashtable;
class Solution {
public int[] twoSum(int[] nums, int target) {
int[] an = new int[2];//结果
Hashtable<Integer,Integer> hs = new Hashtable<Integer,Integer>();
for(int i = 0;i < nums.length;i ++) {
hs.put(nums[i], i);
}
for(int i = 0;i < nums.length;i ++){
int res = target-nums[i];
Integer v = hs.get(res);
if(v != null&&v!=i){
an[0] = i;
an[1] = v;
break;
}
}
return an;
}
}