We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent a9a42da commit 341ffc7Copy full SHA for 341ffc7
javascript/283-Move-Zeroes.js
@@ -7,16 +7,21 @@
7
*/
8
var moveZeroes = function(nums) {
9
10
- const zeroAtTheEnd = Array(nums.length).fill(0);
11
- let left = 0;
12
- for (let i = 0; i < nums.length; i++) {
13
- const isNonZero = nums[i];
14
- if (isNonZero) {
15
- zeroAtTheEnd[left] = nums[i];
+ const arr = new Array(nums.length).fill(0);
+
+ let [left, right] = [0, 0];
+ while (right < nums.length) {
+ const isZero = (nums[right] === 0);
16
+ if (!isZero) {
17
+ arr[left] = nums[right];
18
left++;
19
}
20
21
+ right++;
22
- return zeroAtTheEnd;
23
24
+ return arr;
25
};
26
27
/**
0 commit comments