forked from haoel/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.java
50 lines (46 loc) · 1.24 KB
/
Solution.java
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package IntegerToRoman;
import java.util.*;
/**
* Created by Luyz on 16/8/21.
* https://leetcode.com/problems/integer-to-roman/
*/
public class Solution {
public String intToRoman(int num) {
String result = "";
int place4 = num / 1000;
for (int i = 0; i < place4; i++) {
result += "M";
}
result += lastIntToRoman(num / 100, "C" , "D", "M");
result += lastIntToRoman(num / 10, "X", "L", "C");
result += lastIntToRoman(num, "I", "V", "X");
return result;
}
private String lastIntToRoman(int num, String one, String five, String ten){
num = num % 10;
if (num >= 5){
if (num == 9){
return one + ten;
}
else{
String result = five;
for (int i = 0; i < num - 5; i++) {
result += one;
}
return result;
}
}
else{
if (num == 4){
return one + five;
}
else{
String result = "";
for (int i = 0; i < num; i++) {
result += one;
}
return result;
}
}
}
}