Leetcode 35. Search Insert Position [Java] | Binary search explained

preview_player
Показать описание
This is the leetcode Search Insert Position problem explanation in Java. This question has been asked by companies such as Amazon, Google, Microsoft and Facebook during the coding interviews.

Please write in the comments below which leetcode problem you want me to solve next.

And remember, a leetcode a day keeps unemployment away!

Thanks for watching!

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

Well explained. Would be glad if you can take up the same problem with duplicates.

nikhilm
Автор

Sir if you can explain why we return l, instead of r at the very end.

bradjohnson
Автор

So uh, I came here to find the big brain method to solve this thinking my solution would be at the bottom of the list, but I got 100% with a simple if statement check and for loop. I am not sure I did it right, lol.

Here is my logic:

I check if the target number is less than the element at index 0, if yes, return 0;
I check if the target number is greater than the last element of the index, if yes, I return the array length as that would be highest index number plus one.
Goes into a for loop if none of those conditions are met, checks each element against the target, if it matches the target return i.
If during the loop if the condition met is i > 0 and nums[i-1] is less than target and nums[i] > target return i;

Is this not acceptable?

michaelcosta
Автор

8 минут смотрел, как англичанин говорит по-русски с сильным британским акцентом

kirillprosviryakov