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); + } +} +``` + ### **...** ``` 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); + } +} +``` + ### **...** ``` 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..624e48d251021 --- /dev/null +++ b/solution/0000-0099/0001.Two Sum/Solution.ts @@ -0,0 +1,14 @@ +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); + } +}