罗马转整形


  • 1
    Z

    class Solution {
    public:
    int romanToInt(string s) {

        int i,num=0,res=0,prev=INT_MAX;
        for(i=0;i<s.size();i++)
        {
                  if(s[i]=='I') num=1;
        else if(s[i]=='V') num=5;
        else if(s[i]=='X') num=10;
        else if(s[i]=='L') num=50;
                else if(s[i]=='C') num=100;
                else if(s[i]=='D') num=500;
        else if(s[i]=='M') num=1000;
            if(prev<num) {res+=num;res=res-prev*2;}
            else {res+=num;}
            prev=num;
        }
    return res;
    }
    

    };
    // 调试看变量
    //注意分号在大括号内,注意等于是==


Log in to reply
 

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