0
点赞
收藏
分享

微信扫一扫

LeetCode 12. Integer to Roman

Sky飞羽 2023-09-05 阅读 45


Given an integer, convert it to a roman numeral.

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

answer:

class Solution {
public:
    string intToRoman(int num) {
        string thousand[] = {"","M","MM","MMM"};
        string hundred[] = {"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"};
        string ten[] = {"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"};
        string single[] = {"","I","II","III","IV","V","VI","VII","VIII","IX"};
        int index = 0, mod = 0;
        string result = "";
        index = num / 1000;
        result += thousand[index];
        mod = num % 1000;
        index = mod / 100;
        result += hundred[index];
        mod = mod % 100;
        index = mod /10;
        result += ten[index];
        mod = mod % 10;
        index = mod % 10;
        result += single[index];
         return result;
    }
   
};




举报

相关推荐

0 条评论