a slight variation in two version caused 12ms runtime difference(C++)


  • 0
    N

    class Solution {
    public:
    int romanToInt(string s) {
    int f=10000,p,re=0;
    for(int i=0;i<s.size();i++)
    {
    switch(s[i])
    {
    case 'M':
    p=1000;
    break;
    case 'D':
    p=500;
    break;
    case 'C':
    p=100;
    break;
    case 'L':
    p=50;
    break;
    case 'X':
    p=10;
    break;
    case 'V':
    p=5;
    break;
    default:
    p=1;
    break;
    }
    if(p<=f)
    {
    re+=p;
    f=p;
    }
    else
    {
    re+=p;
    re-=2*f;
    f=p;
    }
    }
    return re;
    }
    };
    This is my code, the runtime is 32ms,but if I delete 'f=p' in else-clause, result is alse correct and runtime is 44ms ,I am curious about the reason.


Log in to reply
 

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