You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

25 lines
474 B
TypeScript

export function twoSumHashTable(nums: number[], target: number): number[] {
const result: number[] = [];
const hashTable: Record<string, number> = {};
let i = 0;
while (i < nums.length) {
let key = nums[i];
let val = i;
let searchKey = String(target - key);
if (searchKey in hashTable) {
result.push(hashTable[searchKey]);
result.push(val);
break;
} else {
hashTable[key] = val;
}
i++
}
return result;
};