forked from andrewdcrypt/LeetCodeStuff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMove Zeroes.php
63 lines (49 loc) · 1.54 KB
/
Move Zeroes.php
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
62
63
#Move Zeroes
Description: Move all zeroes to the end of the array while maintaining the relative order of the non-zero elements
Method One:
set the array length outside of loop
in the loop search for 0 if its zero
append it to $nums and use unset() instead of array_splice()
to remove it from the current position without
changing the array keys
class Solution {
/**
* @param Integer[] $nums
* @return NULL
*/
function moveZeroes(&$nums) {
$arr_len = count($nums);
for($index = 0; $index < $arr_len; $index++){
if ($nums[$index] === 0){
$nums[] = $nums[$index];
unset($nums[$index]);
}
}
return null;
}
}
Method Two:
Create an array to store the zeros
Loop through the array to remove them from $nums
store zeroes into $zero_arr
reset the $nums array keys
set index back 1 so it loops correctly after resetting the $nums array key
set $nums to the array_merge result
/**
* @param Integer[] $nums
* @return NULL
*/
function moveZeroes(&$nums) {
$index = 0;
$zero_arr = [];
while($index < count($nums)){
if ($nums[$index] == 0){
$zero_arr[] = $nums[$index];
unset($nums[$index]);
$nums = array_values($nums);
$index--;
}
$index++;
}
$nums = array_merge($nums,$zero_arr);
}