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?
Code
to_string
轉成字串再用 two pointer 比較。
也可以 reverse 之後再跟原本的比較。
Math
rev
為 x
的後半部,又因為 while(rev < x)
,因此 rev
可能會比 x
大十倍。
要注意 edge case:x = 10
,此時倒轉過來會是 01
,會有 leading zero,所以要檢查 (x % 10 == 0 && x != 0)
。