Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
?
public class Solution {
public String intToRoman(int num) {
Map<Integer, String> map = new HashMap<Integer, String>();
map.put(1, "I");
map.put(4, "IV");
map.put(5, "V");
map.put(9, "IX");
map.put(10, "X");
map.put(40, "XL");
map.put(50, "L");
map.put(90, "XC");
map.put(100, "C");
map.put(400, "CD");
map.put(500, "D");
map.put(900, "CM");
map.put(1000, "M");
int nums[] = {1000,900,500,400,100,90,50,40,10,9,5,4,1};
String res = "";
for (int i = 0; i < nums.length; i++) {
int t = num/nums[i];
num = num%nums[i];
for (int j = 1; j <= t; j++) {
res += map.get(nums[i]);
}
}
return res;
}
}
?
原文:http://hcx2013.iteye.com/blog/2213973