Leetcode 283. Move Zeroes [ Solution + Code Explained]
HTML-код
- Опубликовано: 8 окт 2024
- One of the most frequently asked coding interview questions on Array in companies like Google, Facebook, Amazon, LinkedIn, Microsoft, Uber, Apple, Adobe etc.
LeetCode : Move Zeroes
Question : Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.
Example:
Input: [0,1,0,3,12]
Output: [1,3,12,0,0]
Note:
You must do this in-place without making a copy of the array.
Minimize the total number of operations.
Connect with me on LinkedIn at: / jayati-tiwari
Thas very very easy one , kindly take tougher ones
Thanks for watching the video. Right now I am focusing on providing solution for most asked questions in interviews from LeetCode in their frequency order. Soon I will publish solutions for hard difficulty level question as well. keep coding and stay safe.
@@jayatitiwari444 ya please make harder ones, must avoid easy tag problems , make some harder Tree,LL problems
What inspires you ?
How about something like this
int counter = 0;
for(int i=0;i
This is an easy question you could have experimented with different formats of solutions, there's tons of interesting ideas in the discuss tab even discussing those would be a more worthwhile video.
public void moveZeroes(int[] nums) {
int stockValue = 0,count = 0;
for(int i = 0 ; i < nums.length;i++) {
if(nums[i] != 0) {
stockValue = nums[count];
nums[count] = nums[i];
nums[i] = stockValue;
count++;
}
}
}
Below should be sufficient. No need of nested while loops.
public static int[] move(int[] array) {
int i = 0;
while (i
thanks it's working 😀
why the swap?
This one is very inefficient. Its running in n(n^2) time, you can make it linear.
which compiler you are using ?
She is running her program on leetcode.com
Make tougher ones, this is very easy 🥺
Sure. These are the mostly frequent asked questions in phone screenings. I will now be targeting harder problems. Thanks for the suggestion. :)