leetcode 12
算法介绍:
-
哈希
-
贪心
-
实现代码如下
class Solution {
public:
string intToRoman(int num) {
// 哈希贪心
string s[] = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
int v[] = {1000,900,500,400,100,90,50,40,10,9,5,4,1};
int i = 13;
string res = "";
for(int i = 0;i<13;i++){
int temp = num / v[i];
num -= temp * v[i];
for(int j = 0;j < temp;j++){
res += s[i];
}
}
return res;
}
};