Test Failed
Push — master ( f3f9ec...f43318 )
by Jinyun
02:19
created

HammingDistance   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 12
c 1
b 0
f 0
dl 0
loc 25
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A hammingDistance() 0 9 2
A hammingDistance2() 0 12 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace leetcode;
6
7
class HammingDistance
8
{
9
    public static function hammingDistance(int $x, int $y): int
10
    {
11
        if ($x === $y) {
12
            return 0;
13
        }
14
15
        $z = (string)(decbin($x ^ $y));
16
17
        return (int)(substr_count($z, '1'));
18
    }
19
20
    public static function hammingDistance2(int $x, int $y): int
21
    {
22
        if ($x === $y) {
23
            return 0;
24
        }
25
        [$n, $z] = [0, $x ^ $y];
26
        while ($z) {
27
            $z &= $z - 1;
28
            $n++;
29
        }
30
31
        return $n;
32
    }
33
}
34