13. Roman to Integer Posted on 2020-06-14 | Edited on 2021-01-22 | Views: LeetCode 123456789101112131415161718192021222324252627class Solution { public int romanToInt(String s) { int len=s.length(); int[] tmp=new int[len]; for(int i=0,j=0;i<len;i++){ switch(s.charAt(i)){ case 'I':tmp[j]=1;j++;break; case 'V':tmp[j]=5;j++;break; case 'X':tmp[j]=10;j++;break; case 'L':tmp[j]=50;j++;break; case 'C':tmp[j]=100;j++;break; case 'D':tmp[j]=500;j++;break; case 'M':tmp[j]=1000;j++;break; } } int sum=0; for(int i=0;i<len;i++){ if(i+1<len&&tmp[i]<tmp[i+1]){ sum+=tmp[i+1]-tmp[i]; i++; }else{ sum+=tmp[i]; } } return sum; }}