Skip to content

Commit fd55eb2

Browse files
authored
Create 012_integertoroman.java (qiyuangong#12)
Added JAVA solution for leetcode 012 integertoroman, by @arun-aditya
1 parent b0b0069 commit fd55eb2

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

java/012_integertoroman.java

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

0 commit comments

Comments
 (0)