We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 19cf890 commit 085e3cbCopy full SHA for 085e3cb
RomanToInteger
@@ -0,0 +1,41 @@
1
+class Solution {
2
+ public int symbol_value(char A)
3
+ {
4
+ if(A=='I')
5
+ return 1;
6
+ else if(A=='V')
7
+ return 5;
8
+ else if(A=='X')
9
+ return 10;
10
+ else if(A=='L')
11
+ return 50;
12
+ else if(A=='C')
13
+ return 100;
14
+ else if(A=='D')
15
+ return 500;
16
+ else if(A=='M')
17
+ return 1000;
18
+
19
20
21
+ return 0;
22
+ }
23
+ public int romanToInt(String s) {
24
+ int sum=0,pre_value=0;
25
+ for(int i=0;i<s.length();i++)
26
27
+ int p=symbol_value(s.charAt(i));
28
+ sum+=p;
29
+ if(pre_value==1 && (p==5|| p==10))
30
+ sum-=2;
31
+ else if(pre_value==10 && (p==50|| p==100))
32
+ sum-=20;
33
+ else if(pre_value==100 && (p==500|| p==1000))
34
+ sum-=200;
35
+ pre_value=p;
36
37
38
39
+ return sum;
40
41
+}
0 commit comments