| 1 | class Solution { |
| 2 | public String intToRoman(int num) { |
| 3 | Map<Integer, String> map = new HashMap(); |
| 4 | map.put(1, "I"); map.put(5, "V"); map.put(10, "X"); |
| 5 | map.put(50, "L"); map.put(100, "C"); map.put(500, "D"); map.put(1000, "M"); |
| 6 | map.put(4, "IV"); map.put(9, "IX"); map.put(40, "XL"); map.put(90, "XC"); |
| 7 | map.put(400, "CD"); map.put(900, "CM"); |
| 8 | |
| 9 | int[] sequence = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}; |
| 10 | |
| 11 | StringBuffer sb = new StringBuffer(); |
| 12 | for (int i = 0; i<sequence.length; i++) { |
| 13 | int base = sequence[i]; |
| 14 | |
| 15 | while (num >= base) { |
| 16 | sb.append(map.get(base)); |
| 17 | num -= base; |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | return sb.toString(); |
| 22 | } |
| 23 | } |