LeetCode Two Sum - Java

preview_player
Показать описание
In this video I teach how to solve the Two Sum LeetCode problem. This problem has been asked in several FAANG coding interviews, specially at Google. Please leave a like if you enjoyed!

SUBSCRIBE!

Social
▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬

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

Hi, this was a really helpful video and I'm really new to programming (just finished Data Structures and Algorithms at my college);

To make sure, I understand,

what do you mean by "return nums" being the base case?

Thanks so much!

mini_mouseinthehouse
Автор

Can you start a leetcode series and do it in javascript?

dinoshanromeltar
Автор

It's not submitting successfully, I have written exact code as in the vdo, by nested loops.

amazingbestvideos
Автор

/* Better way to do it using a single loop

i.e [5, 3, 4, 8] target = 8
i = 0 -> 8-5 = 3 (seen key 3 before in the Map?, no add to the map with the index map.put(5, 0)) 5 = nums[i], 0 = i
i = 1 -> 8-3 = 5 (seen key 5 before in the Map?, yes return [0, 1])
*/
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> numberMap = new HashMap();
int length = nums.length;
for(int i=0;i<length;i++){
int difference = target - nums[i];

return new int[]{numberMap.get(difference), i};
}
numberMap.put(nums[i], i);
}
return new int[]{-1, -1};
}

chinthakadevinda
Автор

Can you use a random number generator to check random indexes 😳😳

kakashi
welcome to shbcf.ru