From d11ace0a506eaacbeaa3c0f50832706b8d8b7cd9 Mon Sep 17 00:00:00 2001 From: Abhishek Yadav <150127962+iam-abhishek-yadav@users.noreply.github.com> Date: Fri, 24 Nov 2023 19:14:11 +0530 Subject: [PATCH 1/5] feat: add solutions to lc problem: No.0001 --- solution/0000-0099/0001.Two Sum/Solution.ts | 22 +++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 solution/0000-0099/0001.Two Sum/Solution.ts diff --git a/solution/0000-0099/0001.Two Sum/Solution.ts b/solution/0000-0099/0001.Two Sum/Solution.ts new file mode 100644 index 0000000000000..3aeb76313666b --- /dev/null +++ b/solution/0000-0099/0001.Two Sum/Solution.ts @@ -0,0 +1,22 @@ +function twoSum(nums: number[], target: number): number[] { + // Create a map to store the remaining value needed to reach the target + const map: Map = new Map(); + + // Iterate through the array + for (let i = 0; i < nums.length; i++) { + // Calculate the remaining value needed to reach the target + const rem = target - nums[i]; + + // Check if the remaining value is already in the map + if (map.has(rem)) { + // If found, return the indices of the two numbers + return [i, map.get(rem)!]; + } + + // Store the current number and its index in the map + map.set(nums[i], i); + } + + // If no such pair is found, return an empty array + return []; +} From 0546a805a8bfdbd1fdbb79a330306fd2c1558423 Mon Sep 17 00:00:00 2001 From: Abhishek Yadav <150127962+iam-abhishek-yadav@users.noreply.github.com> Date: Fri, 24 Nov 2023 19:59:37 +0530 Subject: [PATCH 2/5] Update Solution.ts --- solution/0000-0099/0001.Two Sum/Solution.ts | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/solution/0000-0099/0001.Two Sum/Solution.ts b/solution/0000-0099/0001.Two Sum/Solution.ts index 3aeb76313666b..280fd330848ff 100644 --- a/solution/0000-0099/0001.Two Sum/Solution.ts +++ b/solution/0000-0099/0001.Two Sum/Solution.ts @@ -1,22 +1,15 @@ function twoSum(nums: number[], target: number): number[] { - // Create a map to store the remaining value needed to reach the target - const map: Map = new Map(); + const m: Map = new Map(); - // Iterate through the array - for (let i = 0; i < nums.length; i++) { - // Calculate the remaining value needed to reach the target - const rem = target - nums[i]; + for (let i = 0; ; ++i) { + const x = nums[i]; + const y = target - x; - // Check if the remaining value is already in the map - if (map.has(rem)) { - // If found, return the indices of the two numbers - return [i, map.get(rem)!]; + if (m.has(y)) { + return [m.get(y)!, i]; } - // Store the current number and its index in the map - map.set(nums[i], i); + m.set(x, i); } - // If no such pair is found, return an empty array - return []; } From 7e7a1ca34b965169b178510b1ac2c1a8ca7037d9 Mon Sep 17 00:00:00 2001 From: iam-abhishek-yadav Date: Fri, 24 Nov 2023 14:30:42 +0000 Subject: [PATCH 3/5] style: format code and docs with prettier --- solution/0000-0099/0001.Two Sum/Solution.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/solution/0000-0099/0001.Two Sum/Solution.ts b/solution/0000-0099/0001.Two Sum/Solution.ts index 280fd330848ff..624e48d251021 100644 --- a/solution/0000-0099/0001.Two Sum/Solution.ts +++ b/solution/0000-0099/0001.Two Sum/Solution.ts @@ -11,5 +11,4 @@ function twoSum(nums: number[], target: number): number[] { m.set(x, i); } - } From fe52aab7befc135285d4555c747c5fd2c144680a Mon Sep 17 00:00:00 2001 From: Abhishek Yadav <150127962+iam-abhishek-yadav@users.noreply.github.com> Date: Fri, 24 Nov 2023 20:11:37 +0530 Subject: [PATCH 4/5] Update README.md --- solution/0000-0099/0001.Two Sum/README.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/solution/0000-0099/0001.Two Sum/README.md b/solution/0000-0099/0001.Two Sum/README.md index 8cb31ebef6a58..0ebec1acbca10 100644 --- a/solution/0000-0099/0001.Two Sum/README.md +++ b/solution/0000-0099/0001.Two Sum/README.md @@ -255,6 +255,25 @@ class Solution { } ``` +### **TypeScript** + +```ts +function twoSum(nums: number[], target: number): number[] { + const m: Map = new Map(); + + for (let i = 0; ; ++i) { + const x = nums[i]; + const y = target - x; + + if (m.has(y)) { + return [m.get(y)!, i]; + } + + m.set(x, i); + } +} +``` + ### **...** ``` From 4eeb82f72b3f9f28f323b2431e3f28790a147851 Mon Sep 17 00:00:00 2001 From: Abhishek Yadav <150127962+iam-abhishek-yadav@users.noreply.github.com> Date: Fri, 24 Nov 2023 20:11:57 +0530 Subject: [PATCH 5/5] Update README_EN.md --- solution/0000-0099/0001.Two Sum/README_EN.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/solution/0000-0099/0001.Two Sum/README_EN.md b/solution/0000-0099/0001.Two Sum/README_EN.md index c95e80adcd113..e6044cd68c879 100644 --- a/solution/0000-0099/0001.Two Sum/README_EN.md +++ b/solution/0000-0099/0001.Two Sum/README_EN.md @@ -244,6 +244,25 @@ class Solution { } ``` +### **TypeScript** + +```ts +function twoSum(nums: number[], target: number): number[] { + const m: Map = new Map(); + + for (let i = 0; ; ++i) { + const x = nums[i]; + const y = target - x; + + if (m.has(y)) { + return [m.get(y)!, i]; + } + + m.set(x, i); + } +} +``` + ### **...** ```