Test Failed
Push — master ( 5fd8ba...53f60f )
by Jinyun
02:20 queued 17s
created

IntegerToRoman::intToRoman()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 17
rs 9.9332
cc 4
nc 4
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace leetcode;
6
7
class IntegerToRoman
8
{
9
    public static function intToRoman(int $num): string
10
    {
11
        if (empty($num)) {
12
            return '';
13
        }
14
        $numMap = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
15
        $strMap = ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I'];
16
17
        $str = '';
18
        for ($i = 0, $n = count($numMap); $i < $n; $i++) {
19
            while ($num >= $numMap[$i]) {
20
                $num -= $numMap[$i];
21
                $str .= $strMap[$i];
22
            }
23
        }
24
25
        return $str;
26
    }
27
}
28