Passed
Push — master ( 5c1684...54d8c9 )
by Jinyun
03:24
created

BinaryPrefixDivisibleByFive   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 9
eloc 22
c 2
b 0
f 0
dl 0
loc 42
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A prefixesDivByFive2() 0 12 3
A prefixesDivByFive() 0 12 3
A prefixesDivByFive3() 0 12 3
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;
17
            array_push($ans, $num % 5 === 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
    public static function prefixesDivByFive3(array $arr): array
38
    {
39
        if (empty($arr)) {
40
            return [];
41
        }
42
        $num = 0;
43
        foreach ($arr as $key => $val) {
44
            $num = (($num << 1) + $val) % 5;
45
            $arr[$key] = $num === 0;
46
        }
47
48
        return $arr;
49
    }
50
}
51