JAVA solution according to the hint...


  • 0
    public boolean isPalindrome(int x) {
        int i=1;
        if(x<0) return false;
        while(i<=x/10)
        {
            i*=10;
        }
        for(int j=1;j<i;j*=10,i/=10)
        {
            int dh=(x/i)%10;
            int dl=(x/j)%10;
            if(dh!=dl) return false;
        }
        return true;
    }

  • 0

    int i
    int dl
    int dh

    these are not extra space????


  • 0

    Yes, they are extra space. Do you seek the solution with space complexity O(0)? I think it could be a great solution if it really existed.


  • 0

    I didn't mean that. If the palindrome number is very long, the INT or even LONG LONG type cannot store it. Then, you must use a string or other structure to store the number. In this case, the space complexity will be O(length(n)).

    The point is if you can flip the half of the linked list, then not matter how long the list is we still can use the same constant space to solve it.

    Furthermore, the fact that the number given by the problem is stored in a list actually implicit that the length may be too long to store in a INT or whatever variable.


  • 0

    Sorry, I thought this is another problem.


  • 0

    ..So, yeah..it's ok. XD


Log in to reply
 

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