1. two sum
This commit is contained in:
parent
e4d7afc06a
commit
b9046530ac
19
1.two-sum.ts
Normal file
19
1.two-sum.ts
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
// @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
|
||||||
Loading…
x
Reference in New Issue
Block a user