- Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRomanToInteger.java
More file actions
Latest commit
30 lines (29 loc) · 770 Bytes
/
Copy pathRomanToInteger.java
File metadata and controls
30 lines (29 loc) · 770 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
publicclassSolution {
HashMap<Character, Integer> romanToIntMap = newHashMap<Character, Integer>() {
{
put('I', newInteger(1));
put('V', newInteger(5));
put('X', newInteger(10));
put('L', newInteger(50));
put('C', newInteger(100));
put('D', newInteger(500));
put('M', newInteger(1000));
}
};
publicintromanToInt(Strings) {
intret = 0;
inti = 0;
while(i<s.length()) {
if(s.charAt(i)=='I'||s.charAt(i)=='X'||s.charAt(i)=='C') {
if((i+1)<=(s.length()-1)&&romanToIntMap.get(s.charAt(i))<romanToIntMap.get(s.charAt(i+1))) {
ret += romanToIntMap.get(s.charAt(i+1)) - romanToIntMap.get(s.charAt(i));
i = i + 2;
continue;
}
}
ret += romanToIntMap.get(s.charAt(i));
i++;
}
returnret;
}
}