public class Solution {
    public int romanToInt(String s) {

        if(s == null || s.length() == 0) return 0;
        int len = s.length();
        HashMap<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 result = map.get(s.charAt(len -1));
        int pivot = result;
        for(int i = len -2; i>= 0;i--){
            int curr = map.get(s.charAt(i));
            if(curr >=  pivot){
                result += curr;
            }else{
                result -= curr;
            }
            pivot = curr;
        }
        return result;
    }
}

相关文章:

  • 2022-12-23
  • 2021-05-22
  • 2022-02-12
  • 2021-11-24
  • 2021-12-28
  • 2021-11-22
  • 2022-03-01
  • 2022-02-19
猜你喜欢
  • 2021-04-12
  • 2021-12-10
  • 2021-08-11
  • 2022-12-23
  • 2021-12-31
  • 2022-02-15
  • 2021-07-08
相关资源
相似解决方案