题目来源:

https://leetcode.com/problems/palindrome-number/


题意分析:

      这题是要判断一个int是否一个回文数,要求不能申请额外的空间。


题目思路:

      这题也是一个简单的题目,由于不能申请额外的空间,所以不能将int转换成string来处理。根据回文数的定义,我们可以考虑将一个int翻转过来,翻转的时候要注意不能超过32位int的最大值。


代码(python):

 1 class Solution(object):
 2     def isPalindrome(self, x):
 3         """
 4         :type x: int
 5         :rtype: bool
 6         """
 7         if x < 0:
 8             return False
 9         b = x
10         ans = 0
11         while x > 0:
12             ans = ans*10 + x%10
13             if x > 2147483647:
14                 return False
15             x //= 10
16         if ans == b:
17             return True
18         return False
View Code

相关文章:

  • 2022-02-20
  • 2021-08-14
  • 2022-02-08
  • 2021-12-21
  • 2022-03-08
猜你喜欢
  • 2021-06-06
  • 2022-12-23
  • 2021-08-22
  • 2022-12-23
  • 2022-01-13
  • 2022-01-13
  • 2022-12-23
相关资源
相似解决方案