leetcode/1.two-sum.ts
2025-07-29 15:05:24 +02:00

20 lines
425 B
TypeScript

// @leet start
function twoSum(nums: number[], target: number): number[] {
const m: Map<number, number> = new Map();
for (let index = 0; index < nums.length; index++) {
const item = nums[index];
const resInMap = m.get(item);
if (resInMap !== undefined) {
console.log("early stopping");
return [resInMap, index];
} else {
const res = target - item;
m.set(res, index);
}
}
return [];
}
// @leet end