Skip to content

Commit

Permalink
Create MoveZeros
Browse files Browse the repository at this point in the history
  • Loading branch information
abhishektripathi66 authored Jun 11, 2024
1 parent 01ad86e commit 5280cb2
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions Leetcode/MoveZeros
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**

Given an integer array nums, move all 0's to the end of it while maintaining the relative order of the non-zero elements.

Note that you must do this in-place without making a copy of the array.



Example 1:

Input: nums = [0,1,0,3,12]
Output: [1,3,12,0,0]
Example 2:

Input: nums = [0]
Output: [0]
**/

class MoveZeros {
public void moveZeroes(int[] nums) {
int k=0;
for(int i=0;i<nums.length;i++){
if(nums[i]!=0){

nums[k]=nums[i];
k++;
}
}

for(int i=k;i<nums.length;i++){
nums[i]=0;
}

}
}

0 comments on commit 5280cb2

Please sign in to comment.