What's wrong with my code?it's too slowly,64ms.


  • 0
    U
    int romanToInt(string s)
    {
    	int sum = 0;
    	int temp;
    	int oldtemp = 0;
    
    	for (int n = s.size() - 1; n >= 0; --n)
    	{
    		switch (s[n])
    		{
    			case 'I': temp = 1; break;
    			case 'V': temp = 5; break;
    			case 'X': temp = 10; break;
    			case 'L': temp = 50; break;
    			case 'C': temp = 100; break;
    			case 'D': temp = 500; break;
    			case 'M': temp = 1000; break;
    		}
    
    		if (temp >= oldtemp)
    		{
    			sum += temp;
    		}
    		else
    		{
    			sum -= temp;
    		}
    
    		oldtemp =temp;
    	}
    	return sum;
    }
    

    i saw others people use similar methods just consume 36ms.


  • 1
    T

    Hi,
    Your code is fine. The runtime varies between different runs, and your difference is within the variance. However if you see a consistent large difference, then it would indicate something's wrong in your code.


  • 0
    U

    thanks for your answer,~~


  • 0
    U

    i submitted my code again, it runs 36ms. nice~


Log in to reply
 

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