9. Palindrome Number

来源:互联网 发布:华为云计算培训 编辑:程序博客网 时间:2024/05/21 05:38

Determine whether an integer is a palindrome. Do this without extra space.

click to show spoilers.

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. 

 概述:判断一个整数是否是回文

要点:负数不是回文

不允许使用额外的空间,即不可转成字符串

不能翻转,可能导致溢出

class Solution(object):    def isPalindrome(self, x):        input = x        if input>pow(2,31)-1 or input < 0:            return False        len = 1        while(input/len>=10):          len *= 10        while(input):          if input%10 != input/len:            return False          else:            input = (input%len)/10            len = len/100        return True



原创粉丝点击