C++ Solution


  • 0
    X

    class Solution {
    public:
    int romanToInt(string s) {
    //I=1,v=5,x=10,L=50,C=100.D=500,M=1000;
    int num=0,last=0,now;
    for(int i=0;i<s.size();i++){
    switch(s[i]){
    case 'i':
    case 'I': now=1;break;
    case 'v':
    case 'V': now=5;break;
    case 'x':
    case 'X': now=10;break;
    case 'l':
    case 'L': now=50;break;
    case 'c':
    case 'C': now=100;break;
    case 'd':
    case 'D': now=500;break;
    case 'm':
    case 'M': now=1000;break;
    default:break;
    }

            if(last!=0 && last<now) num-=2*last;
          
            num+=now;
            last=now;
           
        }
         return num;
    }
    

    };


Log in to reply
 

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