Integer to Roman

[原题链接-M] (https://leetcode.com/problems/integer-to-roman/description/)

Example
罗马数字问题

时间复杂度: O(N)- 空间复杂度: O(1)

这个题关键点在于穷举

print_r(test(1994));

function test($num) {
    $lookup = [
            'M' => 1000, 
            'CM'=> 900, 
            'D'=> 500, 
            'CD'=> 400, 
            'C'=> 100, 
            'XC'=> 90, 
            'L'=> 50, 
            'XL'=> 40, 
            'X'=> 10, 
            'IX'=> 9, 
            'V'=> 5, 
            'IV'=> 4, 
            'I'=> 1
        ];
        
    $roman = '';
    foreach ($lookup as $key => $value) {
        while ($num >= $value) {
            $roman .= $key;
            $num -= $value;
        }
    }
    
    return $roman;
}    

你可能感兴趣的:(Integer to Roman)