Remove Element @ LeetCode (Python)
Set Matrix Zeroes @ LeetCode (Python)

Palindrome Number @ LeetCode (Python)

kitt posted @ 2014年2月25日 03:31 in LeetCode , 3074 阅读

 

class Solution:
    # @return a boolean
    def isPalindrome(self, x):
        if x < 0: return False
        k = 1
        while x / k >= 10: k *= 10
        while x > 0:
            if x / k != x % 10: return False
            x = (x - x / k * k) / 10
            k /= 100
        return True
Avatar_small
yzl232 说:
2014年6月18日 10:43

class Solution:
# @return a boolean
def isPalindrome(self, x):
if x < 0: return False
s = str(x)
return s == s[::-1]

转换成字符串就好了。 不需要用到那个解法。

Avatar_small
kitt 说:
2014年6月18日 16:24

题目要求Do this without extra space @yzl232:

Avatar_small
dagger 说:
2014年10月28日 23:59

Do this without extra space是什么意思啊,我理解的意思是不许用额外的变量

Avatar_small
lyn 说:
2015年2月22日 03:09

请教一下,
if x / k != x % 10
x/k 是float吗?那么x/k应该不会等于x%10的吧?
我以为整除应该总是用//,但是你好多题解都是用/而且是对的,所以我感到比较费解。。谢谢~

Avatar_small
lyn 说:
2015年2月22日 11:46

@lyn: 查到了,是版本问题:
http://www.answers.com/Q/What_is_floor_division_in_Python
我是从Python3.x开始学的所以会很费解,OJ应该用的是2.x的。
thanks!


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter