题目链接
https://leetcode.com/problems/palindrome-number/
题目原文
Determine whether an integer is a palindrome. Do this without extra space.
题目翻译
判断一个整数(integer)是否是回文,不要使用额外的空间。
思路方法
该题目还有一些官方的提示:
Some hints:
Could negative integers be palindromes? (ie, -1)If you are thinking of converting the integer to string, note the restriction of using extra space.
You could also try reversing an integer. However, if you have solved the problem “Reverse Integer”, you know that the reversed integer might overflow. How would you handle such case?
There is a more generic way of solving this problem.
大概就是告诉我们:1,负数都不是回文数;2,不能通过将数字转为字符串来判断回文,因为使用了额外的空间(即只能使用空间复杂度 O(1) 的方法);3,注意整数溢出问题;4,这个问题有一个比较通用的解法。

这篇博客探讨了如何在Python中不使用额外空间判断一个整数是否为回文。内容包括题目要求、官方提示、多种解题思路以及代码实现。思路包括比较反转整数和原整数、反转一半数字以及逐个比较最前最后的数字。博客作者提醒注意溢出问题,并欢迎反馈指正。
最低0.47元/天 解锁文章
255





