-
Notifications
You must be signed in to change notification settings - Fork 1
/
IntegerToRoman.java
38 lines (32 loc) · 1.14 KB
/
IntegerToRoman.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
package com.smlnskgmail.jaman.leetcodejava.medium;
// https://leetcode.com/problems/integer-to-roman/
public class IntegerToRoman {
private int num;
public IntegerToRoman(int input) {
this.num = input;
}
public String solution() {
StringBuilder result = new StringBuilder();
num = append(result, num, "M", 1000);
num = append(result, num, "CM", 900);
num = append(result, num, "D", 500);
num = append(result, num, "CD", 400);
num = append(result, num, "C", 100);
num = append(result, num, "XC", 90);
num = append(result, num, "L", 50);
num = append(result, num, "XL", 40);
num = append(result, num, "X", 10);
num = append(result, num, "IX", 9);
num = append(result, num, "V", 5);
num = append(result, num, "IV", 4);
num = append(result, num, "I", 1);
return result.toString();
}
private int append(StringBuilder builder, int num, String letter, int letterValue) {
while (num >= letterValue) {
builder.append(letter);
num -= letterValue;
}
return num;
}
}