Leetcode 12 Integer to Roman

2018-01-12 15:10:00 浏览数 (1)

Given an integer, convert it to a roman numeral.

Input is guaranteed to be within the range from 1 to 3999.

模拟题,主要是搞清楚罗马数字的写法和规则,然后打个表就可以了。

代码语言:javascript复制
class Solution {
public:
    string intToRoman(int num) 
    {
        string table[4][11]={
            {"", "I","II","III","IV","V","VI","VII","VIII","IX"},
            {"", "X", "XX","XXX","XL","L","LX","LXX","LXXX","XC"},
            {"", "C","CC","CCC","CD","D","DC","DCC","DCCC","CM"},
            {"", "M", "MM","MMM"}
        };
        string result = "";  
        int now=0;  
        while (num > 0) 
        {  
            result=table[now][num] result;
            num/=10;  
            now  ;  
        }  
        return  result;
    }
};

0 人点赞