Skip to content

Instantly share code, notes, and snippets.

@rdtr
Created January 4, 2016 17:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rdtr/6844564e71958d7cbdf2 to your computer and use it in GitHub Desktop.
Save rdtr/6844564e71958d7cbdf2 to your computer and use it in GitHub Desktop.
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