From b9046530aca9464a1d4073d5d2f84b26813478fc Mon Sep 17 00:00:00 2001 From: Dmitri Date: Tue, 29 Jul 2025 15:05:24 +0200 Subject: [PATCH] 1. two sum --- 1.two-sum.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 1.two-sum.ts diff --git a/1.two-sum.ts b/1.two-sum.ts new file mode 100644 index 0000000..d30d053 --- /dev/null +++ b/1.two-sum.ts @@ -0,0 +1,19 @@ +// @leet start +function twoSum(nums: number[], target: number): number[] { + const m: Map = 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