Skip to content

Commit e4a87e7

Browse files
authored
Update 012. Integer to Roman.java
1 parent ba374a1 commit e4a87e7

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

012. Integer to Roman.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// much inelegant!
1+
// much inelegant! but extensible
22

33
// https://leetcode.com/problems/integer-to-roman
44
public class Solution {
@@ -38,3 +38,14 @@ public String intToRoman(int num) {
3838
return out.toString();
3939
}
4040
}
41+
42+
43+
// quickie, however not extensible
44+
// https://discuss.leetcode.com/topic/39799/simple-java-solution
45+
public String intToRoman(int num) {
46+
String M[] = {"", "M", "MM", "MMM"};
47+
String C[] = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
48+
String X[] = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
49+
String I[] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
50+
return M[num/1000] + C[(num%1000)/100]+ X[(num%100)/10] + I[num%10];
51+
}

0 commit comments

Comments
 (0)