# My solution for this question but I don't know is there any easier way?

• count every Symbol and add its value to the sum, and minus the extra part of special cases.

``````public int romanToInt(String s) {
int sum=0;
if(s.indexOf("IV")!=-1){sum-=2;}
if(s.indexOf("IX")!=-1){sum-=2;}
if(s.indexOf("XL")!=-1){sum-=20;}
if(s.indexOf("XC")!=-1){sum-=20;}
if(s.indexOf("CD")!=-1){sum-=200;}
if(s.indexOf("CM")!=-1){sum-=200;}

char c[]=s.toCharArray();
int count=0;

for(;count<=s.length()-1;count++){
if(c[count]=='M') sum+=1000;
if(c[count]=='D') sum+=500;
if(c[count]=='C') sum+=100;
if(c[count]=='L') sum+=50;
if(c[count]=='X') sum+=10;
if(c[count]=='V') sum+=5;
if(c[count]=='I') sum+=1;

}

return sum;

}``````

• My solution, only scan string once

``````public static int romanToInt(String s) {
int res = 0;
for (int i = s.length() - 1; i >= 0; i--) {
char 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;
}``````

• My solution, scan string once from tail to head. If s[i]'s corresponding integer is no less than the previous one, add it to the result; otherwise, subtract it from the result.

``````    int romanToInt(string s) {
unordered_map<char, int> charToInt = { { 'I', 1 }, { 'V', 5 }, { 'X', 10 }, { 'L', 50 }, { 'C', 100 }, { 'D', 500 }, { 'M', 1000 } };
int result = 0;
int index = s.length() - 1;
int preInt = 0;
while (index >= 0)
{
char ch = s[index];
int curInt = charToInt[ch];
if (curInt >= preInt)
result += curInt;
else
result -= curInt;

preInt = curInt;
index--;
}

return result;
}``````

• Yes, there are easier way of doing it. Scan the string one time with switch check for seven cases. Only need to note that for `I`, `X` and `C`, these three letters can be placed before other letters to form subtraction. So every time we have one of these three letters, we need to check the letter behind this one to see if it is a subtraction. If it is, jump over the next one after calculation.

• I think your solution is quite nice. I use a hashmap to look up for the character's values. It surely takes time and space.

• ``````class Solution:
def romanToInt(self, s):
roman_dict = {'I':1, 'V':5, 'X':10, 'L':50, 'C':100, 'D':500, 'M':1000}
roman_ixc = {'I':5, 'X':50, 'C':500}
value = 0
for i in reversed(s):
if i in roman_ixc and value >= roman_ixc[i]:
value -= roman_dict[i]
else:
value += roman_dict[i]
return value``````

• I think your solution is quite nice

• Your python implementation looks very neat. However, the run time based on online judge is 828ms, which is slower than my ugly implementation that costs 684ms. It seems the performance of dictionary suffers a little bit here.

• ``````public int romanToInt(String s) {
Map<Character, Integer> map = new HashMap<Character, Integer>();
map.put('I', 1);
map.put('V', 5);
map.put('X', 10);
map.put('L', 50);
map.put('C', 100);
map.put('D', 500);
map.put('M', 1000);

int number = 0;
char prev = '#';
for (int i = s.length() - 1; i >= 0; i--) {
char ch = s.charAt(i);
int value = map.get(ch);
if (value < number && ch != prev) {
number = number - value;
} else {
number = number + value;
}
prev = ch;
}
return number;

}``````

• The reason why c++ rocks

``````class Solution {
public:

int romanToInt(string s) {
int result=0;
int i;
map<char,int>m;
m['M']=1000;
m['D']=500;
m['C']=100;
m['L']=50;
m['X']=10;
m['V']=5;
m['I']=1;
for(i=0;i<s.size()-1;++i) {
if(m[s[i]]<m[s[i+1]])
result-=m[s[i]];
else
result+=m[s[i]];}
result+=m[s[i]];
return result;
}
};``````

• This post is deleted!

• This post is deleted!

• Here is my solution.

1. translate the roman string to integer array. for example, MDXCI -> array{ 1000, 500, 10, 100, 1}.

2. let array[i] = - array[i + 1] if array[i] < array[i+1].

int romanToInt(string s) {
int i, len = s.length(), res = 0;
int int_array[len];
for (i = 0; i < len; i++) {
switch (s[i]) {
case 'I':
int_array[i] = 1; break;
case 'V':
int_array[i] = 5; break;
case 'X':
int_array[i] = 10; break;
case 'L':
int_array[i] = 50; break;
case 'C':
int_array[i] = 100; break;
case 'D':
int_array[i] = 500; break;
case 'M':
int_array[i] = 1000; break;
}
}
for (i = 0; i < len; i++) {
if (i + 1 < len && int_array[i] < int_array[i + 1]) {
int_array[i] = -int_array[i];
}
res += int_array[i];
}
return res;
}

• Here is my Python code:

``````class Solution:
# @return an integer
def romanToInt(self, s):
map={'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000}
pre=100000
ret=0
for i in s:
cur=map[i]
if cur>pre:
ret=ret-pre-pre+cur
else:
ret=ret+cur
pre=cur
return ret``````

• This is my code in c++:

``````class Solution {
public:
int romanToInt(string s) {
unordered_map<char, int> romans = { { 'I', 1 }, { 'V', 5 }, { 'X', 10 }, { 'L', 50 }, { 'C', 100 }, { 'D', 500 }, { 'M', 1000 } };
int sum = 0;
int cur, nex;
for( int i = 0; i < s.length() - 1; i++ )
{
cur = romans[ s[ i ] ];
nex = romans[ s[ i + 1 ] ];
if( cur >= nex ) sum += cur;
else sum -= cur;
}
return sum + romans[ s[ s.length() - 1 ] ];
}
};
``````

and this one in Python, they are the same i only a translation:

``````class Solution:
# @return an integer
def romanToInt(self, s):
romans = { 'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000 }
sum = 0
for i in range( len( s ) - 1 ):
cur = romans[ s[ i ] ]
nex = romans[ s[ i + 1 ] ]
if( cur >= nex ):
sum += cur
else:
sum -= cur
return sum + romans[ s[ len( s ) - 1 ] ]
``````

• "IV", "IX"... exists no more than once????

• This is my code in C++.

``````int romanToInt(string s) {
int base[128];
memset(base, 1, sizeof base);
base['I'] = 1;
base['V'] = 5;
base['X'] = 10;
base['L'] = 50;
base['C'] = 100;
base['D'] = 500;
base['M'] = 1000;
base['i'] = 5;
base['x'] = 50;
base['c'] = 500;

int value = 0;
for (auto itr = s.rbegin(); itr != s.rend(); ++itr)
{
if (value >= base[*itr | 0x20]) // eg. I -> i
value -= base[*itr];
else value += base[*itr];
}
return value;
}``````

• same with you

• The solution looks neat and the code is simple. But you have to compare every character with the next one though only 'I', 'X' and 'C' need to be concerned.

• here is my :

``````class Solution:

def judge(self,l,r):
if r == "V" or r == "X":
if l != "I":return -1
if r == "L" or r == "C":
if l != "X" :return -1
if r == "D" or r == "M" :
if l != "C" :return -1
return 0

# @return an integer
def romanToInt(self, s):
romanMap={
"I":1,
"V":5,
"X":10,
"L":50,
"C":100,
"D":500,
"M":1000
}

value = 0
idx = 0
while idx < len(s) :
if idx < len(s)-1:
l = s[idx]
r = s[idx+1]
vl = romanMap[l]
vr = romanMap[r]
if vr > vl :
if self.judge(vl,vr) != 0 :return -1
value = value + vr - vl
idx = idx + 2
else :
value = value + vl
idx = idx +1
else :
value = value + romanMap[s[idx]]
idx = idx +1

return value``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.