-
Notifications
You must be signed in to change notification settings - Fork 1
/
283-Move Zeroes.js
61 lines (54 loc) · 1.46 KB
/
283-Move Zeroes.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* Problem link: https://leetcode.com/problems/move-zeroes
* 283-Move Zeroes
* Solution: Array, pointer
*
* Given an integer array nums, move all 0's to the end of it while maintaining the relative order of the non-zero elements.
* Note: you must do this in-place without making a copy of the array.
*
* Input: nums = [0,1,0,3,12]
* Output: [1,3,12,0,0]
*
* Input: nums = [0]
* Output: [0]
*
*/
/**
* @param {number[]} nums
* @return {void} Do not return anything, modify nums in-place instead.
*/
var moveZeroes = function(nums) {
let pos = 0;
for(let i=0; i<nums.length; i++ ) {
if (nums[i] == 0 ) {
pos = pos+1;
}
else if(pos > 0) {
nums[i-pos] = nums[i];
nums[i] = 0;
}
}
return nums;
};
/**
* @param {number[]} nums
* @return {void} Do not return anything, modify nums in-place instead.
*/
var moveZeroesInitialIdea = function(nums) {
const len = nums.length;
if (len == 1) {
return nums;
}
let pos = -1;
for(let i=0; i<len; i++ ) {
if (nums[i] != 0 & pos > -1) {
[nums[pos], nums[i]] = [nums[i], nums[pos]]
pos = pos+1;
} else if(nums[i] == 0 & nums[i] != nums[i-1]) {
pos = i;
}
}
return nums;
};
console.log("result 1: ", moveZeroes([0,1,0,3,12])); [1,3,12,0,0]
console.log("result 2: ", moveZeroes ([0])); // [0]