import java.util.*;classSolution{publicintromanToInt(String s){int sum =0;int preNum =getValue(s.charAt(0));for(int i =1;i < s.length(); i ++){int num =getValue(s.charAt(i));if(preNum < num){
sum -= preNum;}else{
sum += preNum;}
preNum = num;}
sum += preNum;return sum;}privateintgetValue(char ch){switch(ch){case'I':return1;case'V':return5;case'X':return10;case'L':return50;case'C':return100;case'D':return500;case'M':return1000;default:return0;}}}
作者:donespeak
链接:https://leetcode-cn.com/problems/roman-to-integer/solution/yong-shi-9993nei-cun-9873jian-dan-jie-fa-by-donesp/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
13. 罗马数字转整数import java.util.*;class Solution { public int romanToInt(String s) { int sum = 0; int preNum = getValue(s.charAt(0)); for(int i = 1;i < s.length(); i ++) ...