12. 整数转罗马数字
直接对照Map判断
class Solution {
public String intToRoman(int num) {
String result = "";
HashMap<Integer, String> romanValue = new HashMap<Integer, String>();
romanValue.put(1, "I");
romanValue.put(5, "V");
romanValue.put(10, "X");
romanValue.put(50, "L");
romanValue.put(100, "C");
romanValue.put(500, "D");
romanValue.put(1000, "M");
int figures = 1;
int rem = 0;
while (num != 0) {
rem = num % 10;
if (rem != 0) {
String ch = romanValue.get((rem + 1) * figures);
if (ch == null) {
int count = rem % 5;
for (int i = 0; i < count; i++) {
result = romanValue.get(figures) + result;
}
if (rem >= 5) {
result = romanValue.get(5 * figures) + result;
}
} else {
result = romanValue.get(figures) + ch + result;
}
}
num = num / 10;
figures *= 10;
}
return result;
}
}