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

BinaryPrefixDivisibleByFive   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 15
c 1
b 0
f 0
dl 0
loc 28
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A prefixesDivByFive2() 0 12 3
A prefixesDivByFive() 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) % 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