Leetcode Solution - 1.0 Two Sum | Javascript

preview_player
Показать описание
This is a detail Solution to the Leetcode question Two Sum written in Javascript.
Рекомендации по теме
Комментарии
Автор

This is super helpful as a bootcamp student. I also appreciate seeing the errors happen.

shrfu
Автор

you are amazing, the best leetcode walkthrough so far... so natural, not just pounding straight into problems

humancyyborg
Автор

if the array is sorted, we can use 2 pointers approach as well -

function twoSum(nums, target){
let left =0;
let right = nums.length-1;
while(left < right){
const sum = nums[left] + nums[right];
if(sum == target){
return [left, right];
}
else if(sum > target){
right--;
}
else {
left++;
}
}

return [-1, -1];
}

sreekumarmenon
Автор

Another solution, perhaps a much easier one, would be to have 2 pointers on either end of the array. But this solution was new for me. Thank you.

Onomandah
Автор

Thanks for the video!
I am a beginner, Learned a lot for you.

Amar
Автор

Thanks for the detailed explanation.
Please continue this series 🔥

narayanareddy
Автор

best explanation... You are a monster.

hongkaihuang
Автор

This was really helpful.
(My attempt at interpreting the solution provided)
Halfway through your introduction of the solution I thought you were going to do a first loop where each difference is stored in the map and then a second one where we check each element against the map.
This is an interesting optimisation of this that uses the property that if two numbers add to a third, then removing one number will yield the other (which, based on the question, should be in the list. And no repetition so just one pass was sufficient).

Good stuff!

islamibrahim
Автор

const twoSum = (nums, target) => {
for(i of nums ){
a = target - i
if (a in nums){
return [nums.indexOf(i), nums.indexOf(a)]
}
}
};

altayezekariyas
Автор

That's perfect, very detailed and helpful👍🏻👍🏻👍🏻 thanks

ДениИмагожев
Автор

this was really great. thank you for walking through each step of the logic.

TheSaltyCook
Автор

I find this same approach on other videos too. but thanks your explaination did helped me understand.

javascript_developer
Автор

Wonderful explanation i loved it. KEEP DOING..

PavanKumar-mrtd
Автор

another way if you dont want like this

method :-

const nums = [1, 2, 3, 4, 5, 6, 7, 8, 9];
let target = 5;
var countElements = function(nums, target) {
for(let i=0;i<nums.length;i++){
for(let j=i+1;j<nums.length ;j++){
if(nums[i]+nums[j]=== target){
return [i, j];
}
}
}
}
console.log(countElements(nums, target));

beluga
Автор

Hi.Why you dont use classic if-else in this challenge? I am used to work with if(statement ) then { return something} then else{ return something}.

SuperKojota
Автор

Thank you for content and understanding;)

saymemore
Автор

This is Great!! Subscribed.. Thanks to you for this!

nehaprakash
Автор

Simpler to understand working answer:

nums = [9, 0, 7, 2];
target = 9;

function twoSum(array, target) {
answer = [];
for (let i = 0; i < array.length; i++) {
for (let j = i + 1; j < array.length; j++) {
let sum = array[i] + array[j];
if (sum === target) {
if (array[i] === array[j]) continue;
else {
answer.push(i);
answer.push(j);
return answer;
}
}
}
}
}

console.log(twoSum(nums, target));

simamorpho
Автор

Great stuff, I'm still a little confused but significantly less so! Subscribed! :D

gearsighted
Автор

Definitely not the most efficient solution but this was nice to see as an addnl way to solve it.

JustThink