LEETCODE: Integer to Roman

来源:互联网 发布:java文件压缩中文乱码 编辑:程序博客网 时间:2024/06/06 09:40

知道了罗马数字之后就很简单了。

class Solution {public:    const string romans[4][10] = {        {"", "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 intToRoman(int num) {        string res;        int digit = 0;        while(num != 0){            res = romans[digit][num % 10] + res;            digit ++;            num /= 10;        }                return res;    }};


0 0
原创粉丝点击