018 Integer to Roman
Last updated
Was this helpful?
Last updated
Was this helpful?
Difficulty: Medium
- Tags: Math
, Greedy
Roman numerals are represented by seven different symbols:
I
1
V
5
X
10
L
50
C
100
D
500
M
1000
Given an integer, convert it to a Roman numeral by breaking the integer into its decimal components and mapping them to corresponding Roman symbols.
Example 1:
Input:
Output:
Explanation:
3000 = MMM (M + M + M)
700 = DCC (D + C + C)
40 = XL (X less than L)
9 = IX (I less than X)
Example 2:
Input:
Output:
Explanation:
50 = L
8 = VIII
Example 3:
Input:
Output:
Explanation:
1000 = M
900 = CM
90 = XC
4 = IV
The input num
is in the range [1, 3999].
Difficulty: Medium
- Tags: Math
, Greedy
Roman numerals are represented by seven different symbols:
I
1
V
5
X
10
L
50
C
100
D
500
M
1000
Given an integer, convert it to a Roman numeral by breaking the integer into its decimal components and mapping them to corresponding Roman symbols.
Example 1:
Input:
Output:
Explanation:
3000 = MMM (M + M + M)
700 = DCC (D + C + C)
40 = XL (X less than L)
9 = IX (I less than X)
Example 2:
Input:
Output:
Explanation:
50 = L
8 = VIII
Example 3:
Input:
Output:
Explanation:
1000 = M
900 = CM
90 = XC
4 = IV
The input num
is in the range [1, 3999].
To convert an integer to Roman numerals, we use a list of symbols representing each Roman numeral and their corresponding values. We subtract from the input number in descending order, appending the corresponding symbol to the result string each time.
Java
Time Complexity โณ
O(1): The time complexity is constant since the number of Roman symbols is fixed.
Space Complexity ๐พ
O(1): The space complexity is constant as we only use a few variables and a fixed-size array.
You can find the full solution .