-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntegerToRoman.java
More file actions
31 lines (28 loc) · 1.14 KB
/
IntegerToRoman.java
File metadata and controls
31 lines (28 loc) · 1.14 KB
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
29
30
31
package number.converter;
/**
* Created by Yang on 2017/10/4.
************************************************************************************************
* 12. Integer to Roman
* https://leetcode.com/problems/integer-to-roman/
* 数字转换
* 13. Roman to Integer(罗马数字转阿拉伯数字)
* 660. Remove 9(十进制转九进制)
************************************************************************************************
* Given an integer, convert it to a roman numeral.
* Input is guaranteed to be within the range from 1 to 3999.
************************************************************************************************
*/
public class IntegerToRoman {
public String intToRoman(int num) {
int[] values = {1000,900,500,400,100,90,50,40,10,9,5,4,1};
String[] strs = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
StringBuilder sb = new StringBuilder();
for(int i = 0; i < values.length; i++) {
while(num >= values[i]) {
sb.append(strs[i]);
num -= values[i];
}
}
return sb.toString();
}
}