author: Blankj blog : http://blankj.com time : 2017/04/25 desc :
| 12 | * </pre> |
| 13 | */ |
| 14 | public 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(); |
| 38 | System.out.println(solution.romanToInt("DCXXI"));// 621 |
| 39 | System.out.println(solution.romanToInt("CCCXLVIII"));// 348 |
| 40 | } |
| 41 | } |
nothing calls this directly
no outgoing calls
no test coverage detected