Skip to content

Commit 99ad339

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

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

012. Integer to Roman.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,14 @@ public String intToRoman(int num) {
4242

4343
// quickie, however not extensible
4444
// 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];
45+
public class Solution {
46+
private static String M[] = {"","M","MM","MMM"};
47+
private static String C[] = {"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"};
48+
private static String X[] = {"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"};
49+
private static String I[] = {"","I","II","III","IV","V","VI","VII","VIII","IX"};
50+
51+
public String intToRoman(int num) {
52+
StringBuilder roman = new StringBuilder();
53+
return roman.append(M[num/1000]).append(C[(num%1000)/100]).append(X[(num%100)/10]).append(I[num%10]).toString();
54+
}
5155
}

0 commit comments

Comments
 (0)