# LEETCODE 12. Integer to Roman 解题思路分析

```字符          数值
I             1
V             5
X             10
L             50
C             100
D             500
M             1000```

• `I` 可以放在 `V` (5) 和 `X` (10) 的左边，来表示 4 和 9。
• `X` 可以放在 `L` (50) 和 `C` (100) 的左边，来表示 40 和 90。
• `C` 可以放在 `D` (500) 和 `M` (1000) 的左边，来表示 400 和 900。

```输入: 3

```输入: 4

```输入: 9

```输入: 58

```

```输入: 1994

`{1,4,5,9,10,40,50,90,100,400,500,900,1000}`

`{"I","IV","V","IX","X","XL","L","XC","C","CD","D","CM","M"}`

```public String intToRoman(int num) {
int[] values = new int[]{1,4,5,9,10,40,50,90,100,400,500,900,1000};
String[] symbols =
new String[]{"I","IV","V","IX","X","XL","L","XC","C","CD","D","CM","M"};
// 返回结果
StringBuilder sb = new StringBuilder();
// 从大到小循环每一种罗马数字
for(int i=12;i>=0;i--){
// 如果当前数字大于等于当前罗马数字
if(num>=values[i]){
// 计算当前数字可以兑换多少个罗马数字
int count = num/values[i];
// 打印相应个当前的罗马数字
for(int j=0;j<count;j++){
sb.append(symbols[i]);
}
// 将当前数字取余
num=num%values[i];
}
}
return sb.toString();
}```

Runtime: 4 ms, faster than 96.06% of Java online submissions for Integer to Roman.

Memory Usage: 40.8 MB, less than 11.25% of Java online submissions for Integer to Roman.