Two Sum - LeetCode 1 - JavaScript

preview_player
Показать описание


Step by step walk through on how to solve Two Sum.
And yes it should be complement instead of compliment!

LeetCode

JavaScript

0:00 Explanation
2:39 Code

#softwaredeveloper #coding #leetcode
Рекомендации по теме
Комментарии
Автор

nice and concise explanation. thanks for your short and well planned videos.

khadijaashraf
Автор

I started learning coding this year and your videos have been a great help!

Jenjenson
Автор

Hi, I like this solution, but I also found an interesting solution for this Two Sum on Leetcood that I could not understand:

The question is:

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to the target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

You can return the answer in any order.

The solution is:

var twoSum = function(nums, target) {
const obj = {};

for(let i = 0; i< nums.length; i++) {
let cur = nums[i];

let delta = target - cur;
if(obj[delta] || obj[delta] === 0) {
return [obj[delta], i];
}

obj[cur] = i;
}

};
What is || between obj[delta] || obj[delta, and why do they use it this way?

us-wolf
Автор

What happens in the case where input = [3, 3, 2, 4] and target = 6. The map can only reference 3 once

ajaysrinivas
Автор

Suppose let's take this array
[1, 2, 5, 6, 9]

So, 7 - 1 complement is 6.
We do have 6 in the array right. In that case our solution fails. How to handle that?

kashyapkaki