Test Failed
Push — master ( 329692...6a3bc8 )
by Jinyun
02:24
created

FindNumbersWithEvenNumberOfDigits::findNumbers2()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 19
rs 9.6111
cc 5
nc 6
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace leetcode;
6
7
class FindNumbersWithEvenNumberOfDigits
8
{
9
    public static function findNumbers(array $nums): int
10
    {
11
        if (empty($nums)) {
12
            return 0;
13
        }
14
        $cnt = 0;
15
        foreach ($nums as $num) {
16
            if (strlen((string) $num) % 2 === 0) {
17
                $cnt++;
18
            }
19
        }
20
21
        return $cnt;
22
    }
23
24
    public static function findNumbers2(array $nums): int
25
    {
26
        if (empty($nums)) {
27
            return 0;
28
        }
29
        $cnt = 0;
30
        foreach ($nums as $n) {
31
            $x = 0;
32
            while ($n > 0) {
33
                $n = (int) ($n / 10);
34
                $x++;
35
            }
36
37
            if ($x % 2 === 0) {
38
                $cnt++;
39
            }
40
        }
41
42
        return $cnt;
43
    }
44
}
45