Chinaunix首页 | 论坛 | 博客
  • 博客访问: 255925
  • 博文数量: 170
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1709
  • 用 户 组: 普通用户
  • 注册时间: 2014-05-06 18:01
文章分类

全部博文(170)

文章存档

2016年(11)

2015年(130)

2014年(29)

分类: Java

2015-03-22 16:58:34

//Palindrome Number Total Accepted: 35430 Total Submissions: 122024 My Submissions Question 


Solution 
//Determine whether an integer is a palindrome. Do this without extra space.
public class PalindromeNumber {
public boolean isPalindrome(int x) {
        if(x<0)
        return false;
        if(x==0)
        return true;
        long result=0;
        int m=x;
        while (m>0)
        {
               result = result*10 + m%10;//分解反相加
               m/= 10;
        }
        if(result>2147483647)
        return false;
        if((int)result==x)
        return true;
        else 
return false;

        
    }
public boolean isPalindrome2(int x) {
      if (x < 0)
            return false;
        if (x == 0)
             return true;
            
       int base = 1;
        while(x / base >= 10)
           base *= 10;
            
        while(x!=0)
        {
           int leftDigit = x / base;
            int rightDigit = x % 10;
            if (leftDigit != rightDigit)
                return false;
            
            x -= base * leftDigit;
            base /= 100;
            x /= 10;
        }
        
        return true;
   }

}
阅读(216) | 评论(0) | 转发(0) |
0

上一篇:MaxPoints

下一篇:PascalsTriangle

给主人留下些什么吧!~~