在罗马数字里,除了1000, 900, 500, 400, 100,90, 50, 40, 10, 9, 5, 4, 1 ,其它数字都可以由以上数字“拼接”而成。以上数字的罗马字符为:
所以,对于15,我们找出10和5,放在一起就是XV,16就是10+5+1, XVI(这里一定是从大到小,能够取最大就必须取最大,不能是 10 + 4 + 1 + 1).
1 public class Solution {
2 /**
3 * @param n
4 * The integer
5 * @return Roman representation
6 */
7
8 public String intToRoman(int number) {
9 int[] values = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 };
10 String[] numerals = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" };
11 StringBuilder result = new StringBuilder();
12 for (int i = 0; i ) {
13 while (number >= values[i]) {
14 number -= values[i];
15 result.append(numerals[i]);
16 }
17 }
18 return result.toString();
19 }
20 }