【leetcode】Palindrome Number

来源:互联网 发布:淘宝上九块九包邮 编辑:程序博客网 时间:2024/06/04 18:18

这一题非常的容易,主要容易出错的位置是:

对base 的值进行更新的时候,需要减少两位数。因为之前删除了原数据一头一尾的内容。

另外,不要再代码中使用while(x>0 &&left==right)这样对于10这样的数字判断会出问题。

public class Solution {    public boolean isPalindrome(int x) { int left=0; int right=0;  int base = 1;          while(x / base >= 10)          base *= 10; if(x<0) {return false; } else if(x==0){ return true; } else{while(x>0){right=x%10;left=x/base;x=x-left*base;x/=10;base/=100;if(left!=right){return false;}} }        return true;    }}


0 0
原创粉丝点击