| Category | Difficulty | Likes | Dislikes | 
|---|
| algorithms | Easy (57.09%) | 946 | - | 
Tags
math
Companies
Unknown
判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。
示例 1:
示例  2:
| 1
2
3
 | 输入: -121
输出: false
解释: 从左向右读, 为 -121 。 从右向左读, 为 121- 。因此它不是一个回文数。
 | 
示例 3:
| 1
2
3
 | 输入: 10
输出: false
解释: 从右向左读, 为 01 。因此它不是一个回文数。
 | 
进阶:
你能不将整数转为字符串来解决这个问题吗?
Discussion | Solution
|  1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
 | struct Solution;
// @lc code=start
impl Solution {
    /// ## 解题思路
    pub fn is_palindrome(x: i32) -> bool {
        if x < 0 {
            return false;
        }
        let mut x1 = x;
        let mut rev = 0;
        while x1 > 0 {
            rev = 10 * rev + x1 % 10;
            x1 /= 10;
        }
        return rev == x;
    }
}
// @lc code=end
#[cfg(test)]
mod test {
    use super::*;
    #[test]
    fn test() {
        assert_eq!(Solution::is_palindrome(10), false);
        assert_eq!(Solution::is_palindrome(101), true);
        assert_eq!(Solution::is_palindrome(-101), false);
    }
}
 | 
|  1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
 | lass Solution:
    def isPalindrome(self, x: int) -> bool:
        if x < 0 :
            return False
        def getReserve(x: int) -> int:
            res = 0
            while x > 0:
                res = res * 10 + x % 10
                x = int(x / 10)
            return res
        return x == getReserve(x)
 |