-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathRomanToInteger.java
More file actions
47 lines (41 loc) · 1.05 KB
/
RomanToInteger.java
File metadata and controls
47 lines (41 loc) · 1.05 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
package algorithm.lc;
import java.util.HashMap;
import java.util.Map;
/**
* Given a Roman numeral, convert it to an integer.
*
* Input is guaranteed to be within the range from 1 to 3999.
*
*/
// O(1) space, O(n) time
public class RomanToInteger {
public class Solution {
public int romanToInt(String s) {
// Start typing your Java solution below
// DO NOT write main() function
if (s.length() == 0) {
return 0;
}
Map<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 prev = map.get(s.charAt(0));
int res = prev;
for (int i = 1; i < s.length(); ++i) {
int value = map.get(s.charAt(i));
if (value > prev) { // current unit is bigger than previousC
res += value - 2 * prev;
} else {
res += value;
}
prev = value;
}
return res;
}
}
}