1 class Solution { 2 public: 3 int romanToInt(string s) { 4 char temp; 5 int leng=s.length(); 6 if(leng==0) 7 return 0; 8 int res=charToInt(s.at(leng-1)); 9 for(int i=leng-2;i>=0;i--){ 10 temp=s.at(i); 11 int number=charToInt(temp); 12 if(charToInt(s.at(i))<charToInt(s.at(i+1))) 13 res-=charToInt(s.at(i)); 14 else 15 res+=charToInt(s.at(i)); 16 } 17 return res; 18 19 } 20 int charToInt(char c) 21 { 22 int num=0; 23 switch(c) 24 { 25 case 'I':num=1; 26 break; 27 case 'V':num=5; 28 break; 29 case 'X':num=10; 30 break; 31 case 'L':num=50; 32 break; 33 case 'C':num=100; 34 break; 35 case 'D':num=500; 36 break; 37 case 'M':num=1000; 38 break; 39 } 40 return num; 41 } 42 };