LeetCode(34)-Palindrome Number

来源:互联网 发布:淘宝超低价宝贝集锦 编辑:程序博客网 时间:2024/06/05 15:54

题目:

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;    }}
0 0
原创粉丝点击