LeetCode Two Sum Solution Explained - Java

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

Preparing For Your Coding Interviews? Use These Resources
————————————————————

Other Social Media
----------------------------------------------

Show Support
------------------------------------------------------------------------------

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

So much respect for you. You documented your grind from start to finish!

basedecho
Автор

Thanks, Nick! I think the reason why the execution takes more time at the end of the video is that you are performing the subtraction operation twice, rather than perform it once and only retrieve the value in two places. The original approach you showed is more efficient and the Leetcode compiler seems to agree.

AnshumanBiswas
Автор

Thanks. I am gonna watch all your leetcode videos and give thumbs up to all the videos :)

Shiva-zyjq
Автор

Put "int complement=0" out of the for loop. You would get the output in 1 ms

tarunmendon
Автор

One thing to note: given that nums[i] is the key, if the test array (nums) has duplicate values e.g: [1, 1, 1, 14, 3, 7, 2], you will get an error for adding a new value to the hashmap with a key that already exists. Better to do num_map.put(i, nums[i])

cyrilpatton
Автор

It is faster to keep the subtraction into a var because it only needs to calculate once

lucaspenz
Автор

Thank-you so much I just started learning DSA, your videos are very helpful and your doing a great job.

saileshramesh
Автор

Man you look like big head from Silicon Valley movie series. Good work! Thank you for the video

itheblackwolfofmyfamily
Автор

Is compliment the same as saying answer? I'm confused on why we use that word

ProdHway
Автор

Brute Force Solution:

class Solution {
public int[] twoSum(int[] nums, int target) {
int first, comp;
for(int i=0;i<nums.length;i++){
for(int j=i+1;j<nums.length;j++){
if(nums[i] + nums[j] == target){
int[] ans = {i, j};
return ans;
}
}
}
return nums;
}
}

prakashkrishna
Автор

Thank you so much. You taught me how to solve this and now I know how to use hashmaps.

Thephantom
Автор

i keep running into missing return statment for line 15. Thanks for this video too i understand the algorithm very well. I seem to run into same problem with return statments in all my code .

theindecisivegamer
Автор

Why can't we put the num_map.put(nums[i], i) at the beginning of the for loop? I tried that and it gives runtime error for the input [3, 3]. But if I put it at the end of the for loop like in the video, it's good

annieonee
Автор

Thanks from India!
You really explain very well :)

saumyasingh
Автор

There is a issue in this code now based on newly test added, that what if you have duplicate items in array for example 1, 1, 1, 1, 1, 4, 1, 1, 1, 1, 1, 7, 1, 1, 1, 1, 1 and target is 11

rohitsakalle
Автор

do I Need to write IllegalArgumentException or can i just have a print statement that says "no match found"

surajyerramilli
Автор

in the nested for loop solution, if you remove the throw newIllegal line since it isnt needed, then you get an error that says 'missing return statement', how do you resolve that error in a manner that does not have redundant code. Thank you in advance!

Priyanka-xhgs
Автор

could anyone explain to me how the section: compliment = target - nums[i] works?

ShadwBXr
Автор

why did we go nums.length in the second loop wouldn't there be an error of index outof bounds

theweird_
Автор

Hi I have a question on:

return int[] {num_map.get(complement), i}

How does it not return [1, 0], since the complement is in index 1 (7) and i is still index 0 (2)

Appreciate the video and help

Mkm-xthx