forked from mengli/leetcode
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntegertoRoman.java
More file actions
Latest commit
28 lines (25 loc) · 657 Bytes
/
Copy pathIntegertoRoman.java
File metadata and controls
28 lines (25 loc) · 657 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/**
* Given an integer, convert it to a roman numeral.
*
* Input is guaranteed to be within the range from 1 to 3999.
*/
publicclassIntegertoRoman {
publicStringintToRoman(intnum) {
Stringa[][] = {
{ "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX" },
{ "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC" },
{ "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM" },
{ "M", "MM", "MMM", "", "", "", "", "", "" } };
Stringresult = "";
intkey = 0;
while (num != 0) {
intd = num - num / 10 * 10;
if (d != 0) {
result = a[key][d - 1] + result;
}
num /= 10;
key++;
}
returnresult;
}
}