LeetCode OJ 513. Find Bottom Left Tree Value
Description
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
解题思路
整数转换为罗马数字,用一个二维数组保存1-9,10-90,100-900,1000-9000,其中每行第一位为空字符串。然后把从高位开始,将其转化为罗马数字表示的字符串。
代码
class Solution {
public:
string intToRoman(int num) {
string res = "";
char *itor[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","XL","L","LX","LXX","LXXX","XC"},
};
res += itor[3][num / 1000 % 10];
res += itor[2][num / 100 % 10];
res += itor[1][num / 10 % 10];
res += itor[0][num % 10];
return res;
}
};