MCPcopy Create free account
hub / github.com/Blankj/awesome-java-leetcode / romanToInt

Method romanToInt

src/com/blankj/easy/_0013/Solution.java:15–34  ·  view source on GitHub ↗
(String s)

Source from the content-addressed store, hash-verified

13 */
14public class Solution {
15 public int romanToInt(String s) {
16 Map<Character, Integer> map = new HashMap<>();
17 map.put('I', 1);
18 map.put('V', 5);
19 map.put('X', 10);
20 map.put('L', 50);
21 map.put('C', 100);
22 map.put('D', 500);
23 map.put('M', 1000);
24 int len = s.length();
25 int sum = map.get(s.charAt(len - 1));
26 for (int i = len - 2; i >= 0; --i) {
27 if (map.get(s.charAt(i)) < map.get(s.charAt(i + 1))) {
28 sum -= map.get(s.charAt(i));
29 } else {
30 sum += map.get(s.charAt(i));
31 }
32 }
33 return sum;
34 }
35
36 public static void main(String[] args) {
37 Solution solution = new Solution();

Callers 1

mainMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected