Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice.
function twoSum(nums: number[], target: number): number[] {const map = new Map<number, number>();for (let i = 0; i < nums.length; i++) {const complement = target - nums[i];if (map.has(complement)) {return [map.get(complement)!, i];}map.set(nums[i], i);}return [];}
Build genuine algorithmic problem-solving skills. No memorization, actually understand the patterns and how they work.
No more blanking out at technical interviews. Learn efficiently with our explanations in TypeScript, Python, Java, C# and C++.
With the patterns and concepts you will learn, you won't be scared of technical interviews anymore.
We offer some free lessons to help you decide if the course is right for you.