Skip to content

Commit 7a60253

Browse files
committed
525 solved
1 parent 3c4a8f5 commit 7a60253

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
class Solution {
4+
5+
/**
6+
* @param Integer[] $nums
7+
* @return Integer
8+
*/
9+
function findMaxLength(array $nums) {
10+
$maxLength = 0;
11+
$count = 0;
12+
$hashMap = [0 => -1];
13+
14+
foreach($nums as $key=> $number) {
15+
16+
if ($number == 0) {
17+
$count --;
18+
} else{
19+
$count ++;
20+
}
21+
22+
if(array_key_exists($count,$hashMap)) {
23+
$maxLength = max($maxLength,$key - $hashMap[$count]);
24+
} else {
25+
$hashMap[$count] = $key;
26+
}
27+
}
28+
29+
return $maxLength;
30+
31+
}
32+
}
33+
34+
35+
$solution = new Solution;
36+
37+
echo $solution->findMaxLength([0,1,0]);
38+

0 commit comments

Comments
 (0)