OneBitAndTwoBitCharacters   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
eloc 13
c 2
b 0
f 0
dl 0
loc 25
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A isOneBitCharacter() 0 10 3
A isOneBitCharacter2() 0 11 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace leetcode;
6
7
class OneBitAndTwoBitCharacters
8
{
9
    public static function isOneBitCharacter(array $bits): bool
10
    {
11
        if (empty($bits)) {
12
            return false;
13
        }
14
        $n = count($bits);
15
16
        return $n <= 2
17
            ? $bits[0] === 0
18
            : self::isOneBitCharacter(array_slice($bits, $bits[0] + 1, $n));
19
    }
20
21
    public static function isOneBitCharacter2(array $bits): bool
22
    {
23
        if (empty($bits)) {
24
            return false;
25
        }
26
        [$i, $n] = [0, count($bits)];
27
        while ($i < $n - 1) {
28
            $i += $bits[$i] === 0 ? 1 : 2;
29
        }
30
31
        return $i === $n - 1;
32
    }
33
}
34