-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSolution.php
45 lines (40 loc) · 1.01 KB
/
Solution.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
<?php
// https://leetcode.com/problems/remove-element/
// https://leetcode.com/explore/learn/card/fun-with-arrays/526/deleting-items-from-an-array/3248/
class Solution
{
/**
* @param Integer[] $nums
* @return Integer
*/
function removeElement(&$nums, $val)
{
$data = [];
$blank = 0;
for ($i = 0; $i < count($nums); $i++) {
if ($nums[$i] == $val) {
$blank += 1;
} else {
array_push($data, $nums[$i]);
}
}
$res = count($data);
for ($i = 0; $i < $blank; $i++) {
array_push($data, "_");
}
$nums = $data;
return $res;
}
}
/*
Input: nums = [3,2,2,3], val = 3
Output: 2, nums = [2,2,_,_]
Input: nums = [0,1,2,2,3,0,4,2], val = 2
Output: 5, nums = [0,1,4,0,3,_,_,_]
*/
// https://www.php.net/manual/en/language.references.php
$nums = [0, 1, 2, 2, 3, 0, 4, 2];
$val = 2;
$obj = new Solution();
$res = $obj->removeElement($nums, $val);
var_dump($res);