class Solution {
public:
string intToRoman(int num) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
map<int, string> dict;
dict[1] = "I";
dict[4] = "IV";
dict[5] = "V";
dict[9] = "IX";
dict[10] = "X";
dict[40] = "XL";
dict[50] = "L";
dict[90] = "XC";
dict[100] = "C";
dict[400] = "CD";
dict[500] = "D";
dict[900] = "CM";
dict[1000] = "M";
string ans;
for (map<int, string>::reverse_iterator it = dict.rbegin(); it != dict.rend(); it++) {
while (num >= it->first) {
num -= it->first;
ans.append(it->second);
}
}
return ans;
}
};
LeetCode Integer to Roman
最新推荐文章于 2021-05-14 10:16:09 发布