MCPcopy
hub / github.com/qiyuangong/leetcode / intToRoman

Method intToRoman

java/012_Integer_to_Roman.java:2–22  ·  view source on GitHub ↗
(int num)

Source from the content-addressed store, hash-verified

1class 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}

Callers

nothing calls this directly

Calls 2

putMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected