56ms,but I got a strange problem with i--


  • -1
    W

    I got a strange problem.

    bool isPalindrome(int x)
    {
    char a[20];
    char i;
    char counter1, counter2;
    if (x<0)
    return false;
    else
    {
    for (i = 0; x != 0; i++)
    {
    a[i] = x % 10;
    x = x / 10;
    }
    if (i == 1)
    return true;

    	counter1 = 0;
    	counter2 = i-1;//<----the prolem is here, when i write i-- then the variable went wrong
    	for (; counter1 <=counter2; counter1++, counter2=counter2-1)
    	{
    		if (a[counter1] != a[counter2])
    		{
    			return false;
    		}
    	}
    	return true;
    }
    

    }


  • 0
    S

    You are actually not suppose to store the integer into String format since the problem notes that you don't use "Extra space". Why it is debatable what this actually means, they definitely don't encourage storing it in a string.


Log in to reply
 

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