Move Zeroes | LeetCode 283 | Facebook Coding Interview Tutorial

preview_player
Показать описание
Move Zeroes solution: LeetCode 283

AFFILIATE LINKS
If you're interested in learning algorithms, these are great resources.

💲 All coupons and discounts 💲

Move Zeroes | LeetCode 283 | Facebook Coding Interview
#movezeroes #leetcode #algorithms #terriblewhiteboard #codinginterview

Click the time stamp to jump to different parts of the video.
00:00 Title
00:07 Problem readout
00:38 Whiteboard solution
03:41 Coding solution
07:42 Result and outro
Рекомендации по теме
Комментарии
Автор

If there are any videos you'd like me to make or if you have any ideas on how to optimize this solution, let me know!

TerribleWhiteboard
Автор

Just a perfect explanation, there lots of videos here, but none of them were as perfect as yours. Thanks!

mahammadkhalilov
Автор

I couldn't figure out how the 0's were transferred to the end but this helped a ton to make that connection, nice.

TonyRox
Автор

Thank you very much! Great video explanation!

irynasherepot
Автор

Just discovered your channel. Your breakdowns and solutions are the best! Thanks!

marhawk
Автор

This solution is so simple and clean, thank you.

RuslanZinovyev
Автор

Much better than the solution on LC +1

arielsashcov
Автор

Hi, there's an improvement in your code:
you don't need to swap every time that explorer is non-zero, you only swap when explorer != 0 and anchor == 0, in this way you achieve the min # of swaps. Think about this case: 0. In such case that all non zeros are in front of 0, we don't need to do any swap and just scanning one round is enough, but your code would do swap for all non-zero leading elements.

ethanlyu
Автор

One optimization is just to check that anchor !== explorer before swapping to minimize the operations. Otherwise great job!

rrinorl
Автор

what if the 0th element is not 0? Would anchor still be initialized to 0?

senayb
Автор

God, what great variable names. Explorer and anchor

protyaybanerjee
Автор

Thank you sir, nice explanation .I watch these in the morning for a nice challenge.
could you please do a video on reversing a singly linked list recursively, with your nice explanations and explaining the callstack.
Thank you again so much. Have a blessed day.

chamnil
Автор

Can we just run through array and send zeros to the end nums.length times?

var moveZeroes = function(nums) {
let index = 0;

for (let i = 0; i < nums.length; i++) {
if (nums[index] === 0) {
nums.splice(index, 1);
nums.push(0);
} else {
index++;
}
}
};

qbmain
Автор

What if the first element in the array is a non zero, wouldn't that mess the whole algorithm up? you would end up swapping two non zero elements

tommy_corey
Автор

I think we just need set anchor= explore, and explorer = 0, no need tmp variable.

tuanasa
Автор

You don't actually need to swap you could make name[explorer] = 0 as you know it's 0

harryzachariou
Автор

awesome explanation ....
here is the c++ code -
class Solution {
public:
void moveZeroes(vector<int>& nums) {
int slow = 0;
for(int fast = 0; fast < nums.size(); fast++) {
if(nums[fast] != 0) {
int temp = nums[fast];
nums[fast] = nums[slow];
nums[slow] = temp;
slow++;
}
}
}
};

ritwik
Автор

What happens if the array doesn't start with a Zero???

suhailmohamed