Description
LINK: https://leetcode.com/problems/palindrome-number
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.
Example 1:1
2Input: 121
Output: true
Example 2:1
2
3Input: -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:1
2
3Input: 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
Follow up:
Coud you solve it without converting the integer to a string?
Solution
1 | static auto _ = []() |
Discussion
一般方法是,把整数反过来,然后比较一下。而且,无需判断越界,因为既然输入没有越界,那么如果输出越界,则肯定不是回文数。
改进的方法是,只反转一半,然后跟剩下的一半进行比较。这样可以节省一半运算量。不过并不本质,因为最多也就进行10次运算吧。。。