题目

Given a roman numeral, convert it to an integer.

Input is guaranteed to be within the range from 1 to 3999.

Subscribe to see which companies asked this question

和12题较为类似

代码

public class Solution {
   public int romanToInt(String s) {
        int[] values = {1000,500,100,50,10,5,1};
        String sroman="MDCLXVI";
        int sum=0;
        for(int i=s.length()-1,i_last=0;i>=0;i--){
            int i_curr=values[sroman.indexOf(s.charAt(i))];
            if( i_curr>=i_last){
                sum+=i_curr;
                i_last=i_curr;
            }else{
                sum-=i_curr;
            }
        }
        return sum;
    }
}

lindsay_bubble
26 声望11 粉丝