#9
Easy Algorithms Palindrome number
Math
60.3% acceptance
Mar 2, 2026
15698
2922
Given an integer x, return true if x is a palindrome, and false otherwise.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn is_palindrome(x: i32) -> bool {
if x < 0 || (x % 10 == 0 && x != 0) {
return false;
}
let mut n = x;
let mut rev = 0;
while n > rev {
rev = rev * 10 + n % 10;
n /= 10;
}
n == rev || n == rev / 10
}
}