Why there is an Exception in thread "main" java.lang.NullPointerException


  • 0
    A

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

        if (s == null || s.length() == 0) return 0;
        HashMap<Character, Integer> hm = new HashMap<Character, Integer>();
        hm.put('I', 1);
        hm.put('v', 5);
        hm.put('X', 10);
        hm.put('L', 50);
        hm.put('C', 100);
        hm.put('D', 500);
        hm.put('M', 1000);
        
       
        int rst = 0;
        int len = s.length();
        for(int i = 0; i<(len-1); i++){
            if(hm.get(s.charAt(i)) >= hm.get(s.charAt(i+1))){
                rst += hm.get(s.charAt(i));
            }else{
                rst -= hm.get(s.charAt(i));
            }
        }
        rst += hm.get(s.charAt(len-1));
        return rst;
        
        
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.