1. Question

确定一个数是否是回文数。要求不使用额外空间。

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

 

2. Solution

如果是负数,就不是回文数。

2.1 reverse integer

采用反转整数的方法,注意处理溢出(采用long存储数据)

public class Solution {
    public boolean isPalindrome(int x) {
        if( x < 0 )
            return false;
        long rev = 0;
        int y = x;
        while( y != 0 ){
            rev = rev * 10 + y % 10;
            y = y / 10;
        }
        if( rev == (long)x )
            return true;
        return false;
    }
}
View Code

相关文章:

  • 2022-01-23
  • 2022-02-20
  • 2021-08-14
  • 2022-02-08
  • 2021-12-21
  • 2022-03-08
猜你喜欢
  • 2022-01-13
相关资源
相似解决方案