File tree 2 files changed +30
-0
lines changed
2 files changed +30
-0
lines changed Original file line number Diff line number Diff line change 20
20
226|[ Invert Binary Tree] ( ./0226-invert-binary-tree.js ) |Easy|
21
21
263|[ Ugly Number] ( ./0263-ugly-number.js ) |Easy|
22
22
264|[ Ugly Number II] ( ./0264-ugly-number-ii.js ) |Medium|
23
+ 283|[ Move Zeroes] ( ./0283-move-zeroes.js ) |Easy|
23
24
345|[ Reverse Vowels of a String] ( ./0345-reverse-vowels-of-a-string.js ) |Easy|
24
25
350|[ Intersection of Two Arrays II] ( ./0350-intersection-of-two-arrays-ii.js ) |Easy|
25
26
387|[ First Unique Character in a String] ( ./0387-first-unique-character-in-a-string.js ) |Easy|
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments