File tree Expand file tree Collapse file tree 1 file changed +3
-10
lines changed Expand file tree Collapse file tree 1 file changed +3
-10
lines changed Original file line number Diff line number Diff line change 3
3
* https://leetcode.com/problems/move-zeroes/
4
4
* Difficulty: Easy
5
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.
6
+ * Given an integer array nums, move all 0's to the end of it while maintaining the
7
+ * relative order of the non-zero elements.
7
8
*
8
9
* Note that you must do this in-place without making a copy of the array.
9
10
*/
15
16
var moveZeroes = function ( nums ) {
16
17
for ( let i = 0 , j = 0 ; i < nums . length ; i ++ ) {
17
18
if ( nums [ i ] !== 0 ) {
18
- swap ( nums , i , j ++ ) ;
19
+ [ nums [ i ] , nums [ j ++ ] ] = [ nums [ j ] , nums [ i ] ] ;
19
20
}
20
21
}
21
-
22
- return nums ;
23
22
} ;
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