|
|
|
@ -24,11 +24,11 @@ function twoSumHashTable(nums: number[], target: number): number[] {
|
|
|
|
|
let m: Map<number, number> = new Map();
|
|
|
|
|
// 单层循环,时间复杂度 O(n)
|
|
|
|
|
for (let i = 0; i < nums.length; i++) {
|
|
|
|
|
let index = m.get(nums[i]);
|
|
|
|
|
let index = m.get(target - nums[i]);
|
|
|
|
|
if (index !== undefined) {
|
|
|
|
|
return [index, i];
|
|
|
|
|
} else {
|
|
|
|
|
m.set(target - nums[i], i);
|
|
|
|
|
m.set(nums[i], i);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return [];
|
|
|
|
|