Tuesday, October 4, 2022

IntToRoman

 This article is for self education,

refer here: https://leetcode.com/explore/interview/card/amazon/76/array-and-strings/2964/


Roman numerals are represented by seven different symbols: IVXLCD and M.

Symbol       Value
I             1
V             5
X             10
L             50
C             100
D             500
M             1000

For example, 2 is written as II in Roman numeral, just two one's added together. 12 is written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V + II.

Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:

  • I can be placed before V (5) and X (10) to make 4 and 9. 
  • X can be placed before L (50) and C (100) to make 40 and 90. 
  • C can be placed before D (500) and M (1000) to make 400 and 900.

Given an integer, convert it to a roman numeral.


to represent a given integer, we look for the largest symbol that fits into it. We subtract that, and then look for the largest symbol that fits into the remainder, and so on until the remainder is 0. Each of the symbols we take out are appended onto the output Roman Numeral string.

For example, suppose we need to make the number 671.

The largest symbol that fits into 671 is D (which is worth 500). The next symbol up, CM, is worth 900 and so is too big to fit. Therefore, we now have the following.

Roman Numeral so far: D
Integer remainder: 671 - 500 = 171

We now repeat the process with 171. The largest symbol that fits into it is C (worth 100).

Roman Numeral so far: DC
Integer remainder: 171 - 100 = 71

Repeating this with 71, we find the largest symbol that fits in is L (worth 50).

Roman Numeral so far: DCL
Integer remainder: 71 - 50 = 21

For 21, the largest symbol that fits in is X (worth 10).

Roman Numeral so far: DCLX
Integer remainder: 21 - 10 = 11

For 11, the largest symbol that fits in is again X.

Roman Numeral so far: DCLXX
Integer remainder: 11 - 10 = 1

Finally, the 1 is represented with a I and we're done.

Roman Numeral so far: DCLXXI
Integer remainder: 1 - 1 = 0

package practice;

public class IntToRoman {
private static final int[] values = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
private static final String[] symbols = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};

public String intToRoman(int num) {
StringBuilder sb = new StringBuilder();
// Loop through each symbol, stopping if num becomes 0.
for (int i = 0; i < values.length && num > 0; i++) {
// Repeat while the current symbol still fits into num.
while (values[i] <= num) {
num -= values[i];
sb.append(symbols[i]);
}
}
return sb.toString();
}

public static void main(String[] args) {

int num = 89;

IntToRoman intToRoman = new IntToRoman();

System.out.println(intToRoman.intToRoman(num));

}
}

No comments:

Post a Comment