-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathRomanToInteger
More file actions
42 lines (37 loc) · 1000 Bytes
/
RomanToInteger
File metadata and controls
42 lines (37 loc) · 1000 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// https://leetcode.com/problems/roman-to-integer/
class Solution {
public int symbol_value(char A)
{
if(A=='I')
return 1;
else if(A=='V')
return 5;
else if(A=='X')
return 10;
else if(A=='L')
return 50;
else if(A=='C')
return 100;
else if(A=='D')
return 500;
else if(A=='M')
return 1000;
return 0;
}
public int romanToInt(String s) {
int sum=0,pre_value=0;
for(int i=0;i<s.length();i++)
{
int p=symbol_value(s.charAt(i));
sum+=p;
if(pre_value==1 && (p==5|| p==10))
sum-=2;
else if(pre_value==10 && (p==50|| p==100))
sum-=20;
else if(pre_value==100 && (p==500|| p==1000))
sum-=200;
pre_value=p;
}
return sum;
}
}