forked from architsingla13/InterviewBit-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRomanToInt.java
More file actions
61 lines (51 loc) · 1.25 KB
/
RomanToInt.java
File metadata and controls
61 lines (51 loc) · 1.25 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package String;
/**
* Author - archit.s
* Date - 07/10/18
* Time - 2:45 PM
*/
public class RomanToInt {
boolean isLess(String A, int left, int right){
return value(A, left) < value(A, right);
}
int value(String A, int pos){
switch(A.charAt(pos)){
case 'M':
return 1000;
case 'D':
return 500;
case 'C':
return 100;
case 'L':
return 50;
case 'X':
return 10;
case 'V':
return 5;
default:
return 1;
}
}
private int romanToInt(String A) {
int result = 0;
for(int i=A.length()-1;i>=0;){
if(i-1>=0 && isLess(A,i-1,i)){
result+= (value(A,i)-value(A,i-1));
i-=2;
}
else{
result+= value(A,i);
i--;
}
if(i<0){
break;
}
}
return result;
}
public static void main(String[] args) {
StringBuilder s=new StringBuilder(),f = new StringBuilder();
s.append(f);
System.out.println(new RomanToInt().romanToInt("MMDCCXLIII"));
}
}