Test Failed
Push — master ( ad489a...da6e2c )
by Jinyun
02:28
created

ExcelSheetColumnNumber   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 16
c 1
b 0
f 0
dl 0
loc 30
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A titleToNumber2() 0 14 4
A titleToNumber() 0 12 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace leetcode;
6
7
class ExcelSheetColumnNumber
8
{
9
    public static function titleToNumber(string $title): int
10
    {
11
        if (empty($title)) {
12
            return 0;
13
        }
14
        [$ans, $n] = [0, strlen($title)];
15
        for ($i = 0; $i < $n; $i++) {
16
            $ans *= 26;
17
            $ans += ord(strtoupper($title[$i])) - ord('A') + 1;
18
        }
19
20
        return $ans;
21
    }
22
23
    public static function titleToNumber2(string $title): int
24
    {
25
        if (empty($title)) {
26
            return 0;
27
        }
28
        [$ans, $map] = [0, []];
29
        for ($i = 1; $i < 27; $i++) {
30
            $map[strtoupper(chr($i + 65 - 1))] = $i;
31
        }
32
        for ($i = 0, $n = strlen($title); $i < $n; $i++) {
33
            $ans = $ans * 26 + $map[strtoupper($title[$i])];
34
        }
35
36
        return $ans;
37
    }
38
}
39