Leetcode 9. Palindrome Number(python)

判断一个整数是否是回文数。不可以用额外的空间

我的思路很简单。就是计算首和尾,检测是否相同

class Solution(object):
    def isPalindrome(self, x):
    	if x<0: return False
    	i,a=0,x
    	while a!=0:
    		a=a/10
    		i+=1
    
    	first_i=10**(i-1)
    	last_i=10
    
    	while first_i>=last_i:
    		first=x/first_i
    		last=x%last_i
    		if first!=last:
    			return False
    		x%=first_i
    		x/=last_i
    		first_i/=100
    	return True
        

  

posted @ 2016-04-05 10:01  colors  阅读(182)  评论(0编辑  收藏  举报