Integer To Roman
Given an integer A, convert it to a roman numeral, and return a string corresponding to its roman numeral version
Note : This question has a lot of scope of clarification from the interviewer. Please take a moment to think of all the needed clarifications and see the expected response using “See Expected Output”
For the purpose of this question, https://projecteuler.net/about=roman_numerals has very detailed explanations.
Input Format
The only argument given is integer A.
Output Format
Return a string denoting roman numeral version of A.
Constraints
1 <= A <= 3999
For Example
Input 1:
A = 5
Output 1:
"V"
Input 2:
A = 14
Output 2:
"XIV"
public class Solution {
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] >= 0) {
num -= values[i];
sb.append(strs[i]);
}
}
return sb.toString();
}
}
Last updated