Skip to content

Commit 2c47dc8

Browse files
committed
Add solution #283
1 parent 275fb90 commit 2c47dc8

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
226|[Invert Binary Tree](./0226-invert-binary-tree.js)|Easy|
2121
263|[Ugly Number](./0263-ugly-number.js)|Easy|
2222
264|[Ugly Number II](./0264-ugly-number-ii.js)|Medium|
23+
283|[Move Zeroes](./0283-move-zeroes.js)|Easy|
2324
345|[Reverse Vowels of a String](./0345-reverse-vowels-of-a-string.js)|Easy|
2425
350|[Intersection of Two Arrays II](./0350-intersection-of-two-arrays-ii.js)|Easy|
2526
387|[First Unique Character in a String](./0387-first-unique-character-in-a-string.js)|Easy|

solutions/0283-move-zeroes.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* 283. Move Zeroes
3+
* https://leetcode.com/problems/move-zeroes/
4+
* Difficulty: Easy
5+
*
6+
* Given an integer array `nums`, move all `0`'s to the end of it while maintaining the relative order of the non-zero elements.
7+
*
8+
* Note that you must do this in-place without making a copy of the array.
9+
*/
10+
11+
/**
12+
* @param {number[]} nums
13+
* @return {void} Do not return anything, modify nums in-place instead.
14+
*/
15+
var moveZeroes = function(nums) {
16+
for (let i = 0, j = 0; i < nums.length; i++) {
17+
if (nums[i] !== 0) {
18+
swap(nums, i, j++);
19+
}
20+
}
21+
22+
return nums;
23+
};
24+
25+
function swap(nums, i, j) {
26+
const temp = nums[i];
27+
nums[i] = nums[j];
28+
nums[j] = temp;
29+
}

0 commit comments

Comments
 (0)