LeetCode - Algorithms - 13. Roman to Integer

Java

JAVA—————-Easy Version To Understand!!!!

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
class Solution {
public int romanToInt(String s) {
if (s==null || s.isEmpty()) return -1;
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);
char[] arr = s.toCharArray();
int len = arr.length;
int sum = map.get(arr[len-1]);
for(int i=0;i<len-1;i++) {
if (map.get(arr[i])>=map.get(arr[i+1])) {
sum += map.get(arr[i]);
}
else {
sum -= map.get(arr[i]);
}
}
return sum;
}
}

Submission Detail

  • 3999 / 3999 test cases passed.
  • Runtime: 91 ms
  • Your runtime beats 20.92 % of java submissions.