Passed
Push — master ( 9af223...da4490 )
by Jinyun
02:23
created

BinaryPrefixDivisibleByFive::prefixesDivByFive2()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 12
rs 10
cc 3
nc 3
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace leetcode;
6
7
class BinaryPrefixDivisibleByFive
8
{
9
    public static function prefixesDivByFive(array $arr): array
10
    {
11
        if (empty($arr)) {
12
            return [];
13
        }
14
        [$ans, $num] = [[], 0];
15
        foreach ($arr as $val) {
16
            $num = ($num * 2 + $val) % 5;
17
            $ans[] = $num === 0;
18
        }
19
20
        return $ans;
21
    }
22
23
    public static function prefixesDivByFive2(array $arr): array
24
    {
25
        if (empty($arr)) {
26
            return [];
27
        }
28
        $num = 0;
29
        foreach ($arr as $key => $val) {
30
            $num = $num * 2 % 5 + $val;
31
            $arr[$key] = $num % 5 === 0;
32
        }
33
34
        return $arr;
35
    }
36
}
37