Easy Java code


  • 0
    T

    '''
    class Solution {

    public int value(char c){
        switch(c){
            case 'I' : return 1;
            case 'V' : return 5;
            case 'X' : return 10;
            case 'L' : return 50;
            case 'C' : return 100;
            case 'D' : return 500;
            case 'M' : return 1000;
            default : return 0;
        }
    }
    
    public int romanToInt(String s) {
        int val = 0;
        int len = s.length();
        for(int i = 0; i<len; i++){
            int s1 = value(s.charAt(i));
            if(i+1<len){
                int s2 = value(s.charAt(i+1));
                if(s1 >= s2)
                    val += s1;
                else
                    val -= s1;
            }
            else
            {
                val+= s1;
            }
        }
    return val;
    }
    

    }
    '''


Log in to reply
 

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