zoukankan      html  css  js  c++  java
  • [LeetCode]题解(python):009-Palindrome Number

     

    题目来源:

    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

    转载请注明出处:http://www.cnblogs.com/chruny/p/4801704.html

  • 相关阅读:
    python
    C++的socket编程学习
    GooglePlay
    GooglePlay
    Admob
    cocos2dx
    cocos2dx
    cocos2dx
    cocos2dx
    浅谈白鹭Egret
  • 原文地址:https://www.cnblogs.com/chruny/p/4801704.html
Copyright © 2011-2022 走看看