Algorithm
Roman numerals are represented by seven different symbols: I, V, X, L, C, D 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 ones 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:
Ican be placed beforeV(5) andX(10) to make 4 and 9.Xcan be placed beforeL(50) andC(100) to make 40 and 90.Ccan be placed beforeD(500) andM(1000) to make 400 and 900.
Given a roman numeral, convert it to an integer.
Example 1:
Input: s = "III" Output: 3 Explanation: III = 3.
Example 2:
Input: s = "LVIII" Output: 58 Explanation: L = 50, V= 5, III = 3.
Example 3:
Input: s = "MCMXCIV" Output: 1994 Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
Constraints:
1 <= s.length <= 15scontains only the characters('I', 'V', 'X', 'L', 'C', 'D', 'M').- It is guaranteed that
sis a valid roman numeral in the range[1, 3999].
Code Examples
#1 Code Example with C Programming
Code -
C Programming
int romanToInt(char* s) {
int n = 0;
char c;
while (c = *s ++) {
switch (c) {
case 'I':
if (*s == 'V') {
n += 4;
s ++;
} else if (*s == 'X') {
n += 9;
s ++;
} else {
n += 1;
}
break;
case 'V':
n += 5;
break;
case 'X':
if (*s == 'L') {
n += 40;
s ++;
} else if (*s == 'C') {
n += 90;
} else {
n += 10;
}
break;
case 'L':
n += 50;
break;
case 'C':
if (*s == 'D') {
n += 400;
s ++;
} else if (*s == 'M') {
n += 900;
s ++;
} else {
n += 100;
}
break;
case 'D':
n += 500;
break;
case 'M':
n += 1000;
break;
default:
break;
}
}
return n;
}
Copy The Code &
Try With Live Editor
Input
Output
#2 Code Example with C++ Programming
Code -
C++ Programming
class Solution {
public:
int romanToInt(string s) {
unordered_mapm({{'I',1}, {'X',10}, {'C',100}, {'M',1000}, {'V',5}, {'L',50}, {'D',500}});
if(s.size() == 0) return 0;
int sum = m[s[s.size() - 1]];
for(int i = s.size() - 2; i >= 0; i--){
if(m[s[i]] >= m[s[i + 1]]) sum += m[s[i]];
else sum -= m[s[i]];
}
return sum;
}
};
Copy The Code &
Try With Live Editor
Input
Output
#3 Code Example with Java Programming
Code -
Java Programming
map = new HashMap<>();
for (int i = 0; i < strings.length; i++) {
map.put(strings[i], value[i]);
}
int idx = 0;
int num = 0;
while (idx < s.length()) {
if (idx + 1 < s.length() && map.containsKey(s.substring(idx, idx + 2))) {
num += map.get(s.substring(idx, idx + 2));
idx += 2;
} else {
num += map.get(s.substring(idx, idx + 1));
idx++;
}
}
return num;
}
}
Copy The Code &
Try With Live Editor
Input
Output
#4 Code Example with Javascript Programming
Code -
Javascript Programming
const romanToInt = function(s) {
let res = 0
for (let i = s.length - 1; i >= 0; i--) {
let c = s.charAt(i)
switch (c) {
case 'I':
res += res >= 5 ? -1 : 1
break
case 'V':
res += 5
break
case 'X':
res += 10 * (res >= 50 ? -1 : 1)
break
case 'L':
res += 50
break
case 'C':
res += 100 * (res >= 500 ? -1 : 1)
break
case 'D':
res += 500
break
case 'M':
res += 1000
break
}
}
return res
}
Copy The Code &
Try With Live Editor
Input
Output
#5 Code Example with Python Programming
Code -
Python Programming
class Solution:
def intToRoman(self, num):
s = "M" * (num // 1000)
s += "CM" if num % 1000 >= 900 else "D" *((num % 1000) // 500)
s += "CD" if num % 500 >= 400 and s[-2:] != "CM" else "C" * ((num % 500) // 100) if num % 500 < 400 else ""
s += "XC" if num % 100 >= 90 else "L" * ((num % 100) // 50)
s += "XL" if num % 50 >= 40 and s[-2:] != "XC" else "X" * ((num % 50) // 10) if num % 50 < 40 else ""
s += "IX" if num % 10 >= 9 else "V" * ((num % 10) // 5)
s += "IV" if num % 5 >= 4 and s[-2:] != "IX" else "I" * ((num % 5) // 1) if num % 5 < 4 else ""
return s
Copy The Code &
Try With Live Editor
Input
Output
#6 Code Example with C# Programming
Code -
C# Programming
namespace LeetCode
{
public class _013_RomanToInteger
{
int Mapping(char ch)
{
switch (ch)
{
case 'I': return 1;
case 'V': return 5;
case 'X': return 10;
case 'L': return 50;
case 'C': return 100;
case 'D': return 500;
case 'M': return 1000;
default: return 0;
}
}
public int RomanToInt(string s)
{
var result = 0;
for (int i = 0; i < s.Length; i++)
{
if (i > 0 && Mapping(s[i]) > Mapping(s[i - 1]))
{
result += Mapping(s[i]) - Mapping(s[i - 1]) * 2;
}
else
{
result += Mapping(s[i]);
}
}
return result;
}
}
}
Copy The Code &
Try With Live Editor
Input
Output