My code can't pass "MDLXX" on leetcode, but it works on my xcode.


  • 0
    J
    int romanToInt(string s) {
        if(s.empty()) return 0;
        string result = "";
        int res = 0;
        map<string, int> C;
        C["C"] = 100; C["CC"] = 200; C["CCC"] = 300; C["CD"] = 400; C["D"] = 500; C["DC"] = 600; C["DCC"] = 700; C["DCCC"] = 800; C["CM"] = 900;
        map<string, int> X;
        X["X"] = 10; X["XX"] = 20; X["XXX"] =30; X["XL"] = 40; X["L"] = 50; X["LX"] = 60; X["LXX"] = 70; X["LXXX"] = 80; X["XC"] = 90;
        map<string, int> I;
        I["I"] = 1; I["II"] = 2; I["III"] = 3; I["IV"] = 4; I["V"] = 5; I["VI"] = 6; I["VII"] = 7; I["VIII"] = 8; I["IX"] = 9;
        int i = 0;
        while(s[i] == 'M'){
            res += 1000;
            i++;
        }
        while(s[i] == 'C' || s[i] == 'D' || s[i] == 'M'){
            result += s[i];
            i++;
        }
        res += C.find(result)->second;
        result = "";
        while(s[i] == 'X' || s[i] == 'L' || s[i] == 'C'){
            result += s[i];
            i++;
        }
        res += X.find(result)->second;
        result = "";
        while((s[i] == 'I' || s[i] == 'V' || s[i] == 'X') && i < s.length()){
            result += s[i];
            i++;
        }
        res += I.find(result)->second;
        return res;     }
    

    when the `input is "MDLXX", I always get wrong answer on leetcode, but when I run my code on Xcode, it shows 1570, which is the right answer, I don't know why and how to fix it, can somebody help me?


  • 0
    X

    use "==" instead of "is", I just meet the exactly same problem.


Log in to reply
 

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