-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRomanToInteger.java
More file actions
43 lines (37 loc) · 1.23 KB
/
Copy pathRomanToInteger.java
File metadata and controls
43 lines (37 loc) · 1.23 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
package LeetcodePractice;
import java.util.HashMap;
import java.util.Map;
// O(n) time, O(1) space. walk from the right, subtract when a smaller numeral sits before a bigger one
public class RomanToInteger {
public static int romanToInt(String s){
Map<Character,Integer> romanMap = new HashMap<>();
romanMap.put('I', 1);
romanMap.put('V', 5);
romanMap.put('X', 10);
romanMap.put('L', 50);
romanMap.put('C', 100);
romanMap.put('D', 500);
romanMap.put('M', 1000);
int total=0;
int previousValue =0;
for(int i = s.length() -1;i>=0;i--){
int currentValue = romanMap.get(s.charAt(i));
if(currentValue<previousValue)
{
total = total - currentValue;
}
else{
total = total + currentValue;
}
previousValue = currentValue;
}
return total;
}
public static void main(String[] args) {
System.out.println(romanToInt("III"));
System.out.println(romanToInt("IV"));
System.out.println(romanToInt("IX"));
System.out.println(romanToInt("LVIII"));
System.out.println(romanToInt("MCMXCIV"));
}
}