Skip to content

Commit 5614de4

Browse files
authored
Merge pull request vJechsmayr#151 from hyphen-suraj/master
Create MoveZeroAtLast.js
2 parents ebffb8c + 876b8ec commit 5614de4

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {void} Do not return anything, modify nums in-place instead.
4+
*/
5+
6+
7+
8+
/**
9+
QUESTION:
10+
11+
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.
12+
13+
14+
Example:
15+
16+
Input: [0,1,0,3,12]
17+
Output: [1,3,12,0,0]
18+
19+
20+
21+
**/
22+
var moveZeroes = function(nums) {
23+
let count=0;
24+
for(let i=0;i<nums.length;i++){
25+
if(nums[i]==0){
26+
count++;
27+
28+
}
29+
30+
else{
31+
let inte=nums[i]
32+
for(let j=i;j>i-count;j--){
33+
nums[j]=nums[j-1];
34+
}
35+
nums[i-count]=inte;
36+
}
37+
}
38+
39+
};

0 commit comments

Comments
 (0)