parent
cc75ef73b6
commit
437b9f9b04
@ -0,0 +1,61 @@ |
||||
Hi, I am solving a problem on leetcode: https://leetcode.com/problems/roman-to-integer/ |
||||
And my code is failing the test:.. The code is: `int romanToInt(char * s){ |
||||
int stringSize; |
||||
stringSize = strlen(s); |
||||
int ans, counter; |
||||
int pos_I=0, pos_V=0, pos_X=0, pos_L=0, pos_C=0, pos_D=0, pos_M=0; |
||||
ans = 0; |
||||
counter = 0; |
||||
for(counter=0; counter<stringSize; counter++) |
||||
{ |
||||
if (s[counter] == 'I') |
||||
{ |
||||
//store the position of I |
||||
pos_I = counter; |
||||
ans = ans + 1; |
||||
|
||||
} |
||||
else if (s[counter] == 'V') |
||||
{ |
||||
pos_V = counter; |
||||
ans = ans + 5; |
||||
if (counter >=1 ) |
||||
{ |
||||
if ((s[counter-1] == 'I') && counter >= 1) |
||||
{ |
||||
ans = ans - 1; |
||||
} } |
||||
} |
||||
else if (s[counter] == 'X') |
||||
{ |
||||
pos_X = counter; |
||||
ans = ans + 10; |
||||
if (counter >=1 ) |
||||
{ |
||||
if ((s[counter-1] == 'I') && (counter >=1)) |
||||
{ |
||||
ans = ans - 1; |
||||
} } |
||||
} |
||||
else if (s[counter] == 'L') |
||||
{ |
||||
pos_L = counter; |
||||
ans = ans + 50; |
||||
if (counter >= 1) |
||||
{ |
||||
if ((s[counter-1] == 'X') && (counter >= 1)) |
||||
{ |
||||
ans = ans - 10; |
||||
} } |
||||
} |
||||
else if (s[counter] == 'C') |
||||
{ |
||||
pos_C = counter; |
||||
ans = ans + 100; |
||||
if (counter >= 1) |
||||
{ |
||||
if ((s[counter-1] == 'X') && (counter>=1)) |
||||
{ |
||||
ans = ans - 10; |
||||
}} |
||||
}` |
Loading…
Reference in new issue