LeetCode 12 Integer to Roman

来源:互联网 发布:足下软件学院校长 编辑:程序博客网 时间:2024/05/29 11:43

12. Integer to Roman

My Submissions
Total Accepted: 60715 Total Submissions: 158049 Difficulty: Medium

Given an integer, convert it to a roman numeral.

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

Subscribe to see which companies asked this question

Show Tags
Show Similar Problems
Have you met this question in a real interview? 
Yes
 
No

Discuss


将10进制数转成罗马数字

直接法:打表

将数字转成字符串,从最低位开始查表,相加合并即可。

class Solution {public:    string intToRoman(int num)    {        string table[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 res;        char str[6];        sprintf(str,"%d",num);        int len=strlen(str);        for (int i=len-1;i>=0;i--)                res = table[len-1-i][str[i]-'0']+res;        return res;    }};



0 0