Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
将不同罗马字符和对应的数字做成字典,然后将依次转化。时间:55ms
代码如下:
class Solution { public: string intToRoman(int num) { if (num < 1||num>3999) return ""; string symbol[] = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" }; int value[] = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 }; string Roman; for (int i = 0; num != 0;i++){ while (num >= value[i]){ Roman += symbol[i]; num -= value[i]; } } return Roman; } };