- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDemo_2.java
More file actions
Latest commit
44 lines (38 loc) · 1.06 KB
/
Copy pathDemo_2.java
File metadata and controls
44 lines (38 loc) · 1.06 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
importjava.util.HashMap;
importjava.util.Map;
classRomanToInteger{
publicintromanToInt(Strings) { // XIV
Map<Character, Integer> map = newHashMap<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);
intresult = 0;
inti=0;
while(i<s.length()){
intcurrentVal = map.get(s.charAt(i));
intnextVal = 0;
if(i+1<s.length()){
nextVal = map.get(s.charAt(i+1));
}
if(nextVal > currentVal){
result += (nextVal - currentVal);
i = i+2;
} else {
result += currentVal;
i++;
}
}
returnresult;
}
}
publicclassDemo_2 {
publicstaticvoidmain(String[] args) {
RomanToIntegerobj = newRomanToInteger();
intn = obj.romanToInt("XCVII");// 97
System.out.println(n);
}
}