-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRough.js
More file actions
58 lines (46 loc) · 1.07 KB
/
Rough.js
File metadata and controls
58 lines (46 loc) · 1.07 KB
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
/* move 0s to end while keeping the order of non 0 the same
const input = [0,1,0,3,12]
*/
// function moveZero(input) {
// let zeroTracker = null; // null | number
//
// for(let i = 0; i<input.length; i++) {
// // first find zeroTracker
// if(zeroTracker === null) {
// if(input[i] === 0) { // [1,0,1]
// zeroTracker = i;
// }
// continue;
// }
//
// // [0,1,0]
// if(input[i] !==0){
// [input[zeroTracker] ,input[i]] = [input[i], input[zeroTracker]];
// zeroTracker = i;
// }
// }
// return input;
// }
function moveZero(input) {
let zeroTracker = 0;
for(let i = 0; i<input.length; i++) {
// [0,1,0]
if(input[i] !==0){
[input[zeroTracker] ,input[i]] = [input[i], input[zeroTracker]];
zeroTracker++;
}
}
return input;
}
console.log(moveZero([0,1,0,3,12]));
/*
[0,1,0,3,10]
[1,0,0,3,12]
[1,3,0,0,12]
[1,3,12,0,0]
[1,0,2,0,3]
[1,0,2,0,3]
[1,2,0,0,3]
[1,2,3,0,0]
*/
// twoSum examples