-
Notifications
You must be signed in to change notification settings - Fork 2
/
IntegertoRoman.java
26 lines (21 loc) · 975 Bytes
/
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
package IntegertoRoman;
class Solution {
public String intToRoman(int num) {
// store all the possible "numbers" and "strings"
int[] nums = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
String[] roman = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
// Using "StringBuilder" (important)
StringBuilder string_builder = new StringBuilder();
// for each possible nums (from big one to small one)
for(int i=0; i < nums.length; i++){
// using while loop -> "num = num - nums[i]"
// note: if "num == nums[i]" -> also "num = num - nums[i]" (be careful)
while( num >= nums[i]){
num = num - nums[i];
// Using "StringBuilder.append(str)"
string_builder.append(roman[i]);
}
}
return string_builder.toString(); // Using "toString()"
}
}