您的位置:首页 > 职场人生

LeetCode(34)-Palindrome Number

2016-04-07 22:10 302 查看

题目:

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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息