LeetCode(34)-Palindrome Number

题目:

Determine whether an integer is a palindrome. Do this without extra space.

思路:

  • 求一个整数是不是回文树。负数不是,0是
  • 要求不适用额外的内存(变量还是可以的),利用求余,除以10,这样y = y×10+余树,比较y和输入值是否相等,判断是不是回文
  • -

代码:

public class Solution {
    public boolean isPalindrome(int x) {
        if(x < 0){
            return false;
        }
        if(x == 0){
            return true;
        }
        if(x > 0){
            int finish = x;
            //用来存放倒叙相乘的结果
            int y = 0;
            while(x != 0){
                y = y*10 + x%10;
                x = x/10;
            }
            return finish == y ? true:false;
        }
        return true;
    }
}

你可能感兴趣的:(LeetCode,算法,面试,number,palindrome)