Created
January 4, 2016 17:09
-
-
Save rdtr/6844564e71958d7cbdf2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
public String intToRoman(int num) { | |
String mapping[][] = { | |
{"", "M", "MM", "MMM"}, | |
{"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"}, | |
{"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"}, | |
{"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"}, | |
}; | |
StringBuilder res = new StringBuilder(); | |
int base = 1000; | |
int mapIdx = 0; | |
while (num > 0) { | |
int d = num / base; | |
res.append(mapping[mapIdx][d]); | |
mapIdx += 1; | |
num %= base; | |
base /= 10; | |
} | |
return res.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment