-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path13.roman-to-integer.java
44 lines (38 loc) · 1.02 KB
/
13.roman-to-integer.java
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
/*
* @lc app=leetcode id=13 lang=java
*
* [13] Roman to Integer
*/
// @lc code=start
class Solution {
public int romanToInt(String s) {
Map<String, Integer> map = new HashMap<>();
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 index = 0;
while (index < s.length()) {
String curr = s.substring(index, index + 1);
int currValue = map.get(curr);
int nextValue = 0;
if (index + 1 < s.length()) {
String next = s.substring(index + 1, index + 2);
nextValue = map.get(next);
}
if (currValue < nextValue) {
result += nextValue - currValue;
index += 2;
} else {
result += currValue;
index += 1;
}
}
return result;
}
}
// @lc code=end