[HAI] twoSumHashtable
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
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;
|
||||
};
|
||||
Reference in New Issue
Block a user