Skip to content

Commit b9f3f55

Browse files
committed
Add: Exercise 283. Move Zeroes
1 parent a62c27d commit b9f3f55

File tree

5 files changed

+117
-0
lines changed

5 files changed

+117
-0
lines changed

283-move-zeroes/README.md

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# 283. Move Zeroes
2+
Given an integer array `nums`, move all 0's to the end of it while maintaining the relative order of
3+
the non-zero elements.
4+
5+
Note that you must do this in-place without making a copy of the array.
6+
7+
## Examples
8+
9+
### Example 1:
10+
Input: `nums` = [0,1,0,3,12]
11+
Output: [1,3,12,0,0]
12+
13+
### Example 2:
14+
Input: `nums` = [0]
15+
Output: [0]
16+
17+
## Constraints
18+
- 1 <= `nums.length` <= 10^4
19+
- -2^31 <= `nums[i]` <= 2^31 - 1

283-move-zeroes/index.js

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* Shifts all zeroes in `nums` to the end while keeping other elements in order.
3+
* Uses `left` and `right` pointers to swap zeroes with non-zero elements.
4+
* Example: [1,4,0,0,5] -> left = 2, right = 4. Swap nums[4] with nums[2].
5+
* Result: [1,4,5,0,0]
6+
* @param {number[]} nums
7+
* @return {void} Do not return anything, modify nums in-place instead.
8+
*/
9+
var moveZeroes = function(nums) {
10+
let i = 0 // Will traverse the nums array
11+
let left = null // left limit of zeroes window
12+
let right = null // right limit of zeroes window
13+
14+
const n = nums.length
15+
16+
while (i < n && right < n) {
17+
if (nums[i] === 0) {
18+
// Initialize left and right when zero is found for the first time
19+
if (left === null) {
20+
left = i
21+
if (right === null) {
22+
right = left + 1
23+
}
24+
i++
25+
continue
26+
}
27+
i++
28+
right++
29+
continue
30+
}
31+
32+
// Continue looking for a zero
33+
if (left === null && right === null) {
34+
i++
35+
continue
36+
}
37+
38+
// Swap num with the zero at the left
39+
[nums[i], nums[left]] = [nums[left], nums[i]]
40+
41+
left++
42+
right++
43+
i = right
44+
}
45+
};
46+
47+
module.exports = moveZeroes

283-move-zeroes/package.json

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "283-move-zeroes",
3+
"version": "1.0.0",
4+
"description": "Given an integer array `nums`, move all 0's to the end of it while maintaining the relative order of the non-zero elements.",
5+
"main": "index.js",
6+
"scripts": {
7+
"start": "node index.js",
8+
"debug": "node --inspect-brk index.js",
9+
"test": "node test.js"
10+
},
11+
"keywords": [],
12+
"author": "",
13+
"license": "ISC"
14+
}

283-move-zeroes/test.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const assert = require('assert')
2+
const moveZeroes = require('./index')
3+
4+
const tt = [
5+
{
6+
nums: [0,1,0,3,12],
7+
expected: [1,3,12,0,0,]
8+
},
9+
{
10+
nums: [0],
11+
expected: [0]
12+
},
13+
{
14+
nums: [0,1],
15+
expected: [1,0]
16+
}
17+
]
18+
19+
for (const t of tt) {
20+
moveZeroes(t.nums)
21+
assert.strict.deepEqual(t.nums, t.expected)
22+
}

README.md

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Leetcode 75 Notes
2+
3+
## `package.json` scripts
4+
```json
5+
{
6+
// ...
7+
"scripts": {
8+
"start": "node index.js",
9+
"debug": "node --inspect-brk index.js",
10+
"test": "node test.js"
11+
},
12+
// ...
13+
}
14+
15+
```

0 commit comments

Comments
 (0)