forked from huailian123/Leetcode-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13_romanToInt.java
More file actions
27 lines (25 loc) · 744 Bytes
/
Copy path13_romanToInt.java
File metadata and controls
27 lines (25 loc) · 744 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
public class Solution {
public int romanToInt(String s) {
HashMap<Character, Integer> map = new HashMap<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);
int result = 0;
int i = 0;
while(i< s.length()){
int j = i+1;
if(j < s.length() && map.get(s.charAt(i)) < map.get(s.charAt(j))){
result += map.get(s.charAt(j))-map.get(s.charAt(i));
i+=2;
} else{
result+=map.get(s.charAt(i));
i++;
}
}
return result;
}
}