Welcome to Subscribe On Youtube
9. Palindrome Number
Description
Given an integer x
, return true
if x
is a palindrome, and false
otherwise.
Example 1:
Input: x = 121 Output: true Explanation: 121 reads as 121 from left to right and from right to left.
Example 2:
Input: x = -121 Output: false Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
Example 3:
Input: x = 10 Output: false Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
Constraints:
-231 <= x <= 231 - 1
Follow up: Could you solve it without converting the integer to a string?
Solutions
Solution 1: Reverse Half of the Number
First, we determine special cases:
- If
, thenx<0 is not a palindrome, directly returnx false
; - If
and the last digit ofx>0 isx , then0 is not a palindrome, directly returnx false
; - If the last digit of
is notx , then0 might be a palindrome, continue the following steps.x
We reverse the second half of
For example, for
Let’s see how to reverse the second half.
For the number
If we continue this process, we will get more reversed digits.
By continuously multiplying the last digit to the variable
In the code implementation, we can repeatedly “take out” the last digit of
The time complexity is
-
class Solution { public boolean isPalindrome(int x) { if (x < 0 || (x > 0 && x % 10 == 0)) { return false; } int y = 0; for (; y < x; x /= 10) { y = y * 10 + x % 10; } return x == y || x == y / 10; } }
-
class Solution { public: bool isPalindrome(int x) { if (x < 0 || (x && x % 10 == 0)) { return false; } int y = 0; for (; y < x; x /= 10) { y = y * 10 + x % 10; } return x == y || x == y / 10; } };
-
class Solution: def isPalindrome(self, x: int) -> bool: if x < 0 or (x and x % 10 == 0): return False y = 0 while y < x: y = y * 10 + x % 10 x //= 10 return x in (y, y // 10)
-
func isPalindrome(x int) bool { if x < 0 || (x > 0 && x%10 == 0) { return false } y := 0 for ; y < x; x /= 10 { y = y*10 + x%10 } return x == y || x == y/10 }
-
function isPalindrome(x: number): boolean { if (x < 0 || (x > 0 && x % 10 === 0)) { return false; } let y = 0; for (; y < x; x = ~~(x / 10)) { y = y * 10 + (x % 10); } return x === y || x === ~~(y / 10); }
-
/** * @param {number} x * @return {boolean} */ var isPalindrome = function (x) { if (x < 0 || (x > 0 && x % 10 === 0)) { return false; } let y = 0; for (; y < x; x = ~~(x / 10)) { y = y * 10 + (x % 10); } return x === y || x === ~~(y / 10); };
-
impl Solution { pub fn is_palindrome(mut x: i32) -> bool { if x < 0 || (x % 10 == 0 && x != 0) { return false; } let mut y = 0; while x > y { y *= 10; y += x % 10; x /= 10; } x == y || x == y / 10 } }
-
class Solution { /** * @param int $x * @return boolean */ function isPalindrome($x) { $str = (string) $x; $str_reverse = strrev($str); return $str === $str_reverse; } }
-
public class Solution { public bool IsPalindrome(int x) { if (x < 0 || (x > 0 && x % 10 == 0)) { return false; } int y = 0; for (; y < x; x /= 10) { y = y * 10 + x % 10; } return x == y || x == y / 10; } }