题目描述
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.
Example 1:
Input: 121
Output: true
Example 2:
Input: -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: 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?
我的解法
解题思路
题目要求是判断一个数是不是palindrome。一个数如果是palindrome,那么它从左往右读和从右往左读都是一样的。并且负数不是palindrome。并且最好在不将整数转换为string的情况下完成。
其实可以用第七题第思路,只要一个数反转过来和它相等,那么它就是palindrome。
实现代码
1 | class Solution { |
✔ Accepted
✔ 11509/11509 cases passed (32 ms)
✔ Your runtime beats 99.62 % of cpp submissions
✔ Your memory usage beats 99.33 % of cpp submissions (8.2 MB)