Operate the num like Stack operation


  • 0
    D
    public boolean isPalindrome(int x) {
       if(x<0) return false;        
        int temp = x;
        int num = 0;
        while (temp != 0) {
            num = num * 10 + temp % 10;
            temp = temp / 10;
        }
        if (num == x) {
            return true;
        }
        return false;
    }

  • 0
    O

    @dick159 I don't really understand the Question


  • 0
    D

    @otoom.doaa-gmail.com it just reverse the number,and return true when it equal to the original number.
    Exp:number 121.
    first,we can get num = 1 by num*10 + temp %10,temp = 12 by temp =temp/10.
    and on the second interation,we can get num = 12 by num%10 + temp%10,and the above iteration's result as the follow result,we can reverse the num 121 to 121,which is equal the original number and it's a true Palindrome


Log in to reply
 

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