# -*- coding: utf8 -*-
'''
__author__ = 'dabay.wang@gmail.com'
https://oj.leetcode.com/problems/palindrome-number/ Determine whether an integer is a palindrome. Do this without extra space. 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. How would you handle such case? There is a more generic way of solving this problem. ===Comments by Dabay===
这里说的不用额外的空间,实际是指不用堆上的空间(程序外使用的内存空间)。
所以,变量这种使用程序栈的空间是可以的。 从两边往中间比较。用除以10的某次方的商,在取模可以得到需要比较的数。
'''
class Solution:
# @return a boolean
def isPalindrome(self, x):
if x < 0:
return False
div = 1
while x / div >= 10:
div = div * 10 div_left = div
div_right = 1 while div_left > div_right:
left = x / div_left % 10
right = x / div_right % 10
if left != right:
return False
div_left = div_left / 10
div_right = div_right * 10 return True def main():
s = Solution()
print s.isPalindrome(101232101) if __name__ == "__main__":
import time
start = time.clock()
main()
print "%s sec" % (time.clock() - start)

[LeetCode][Python]Palindrome Number的更多相关文章

  1. Leetcode练习题 Palindrome Number

    9. Palindrome Number Question: Determine whether an integer is a palindrome. An integer is a palindr ...

  2. Leetcode 9. Palindrome Number(水)

    9. Palindrome Number Easy Determine whether an integer is a palindrome. An integer is a palindrome w ...

  3. leetcode 9 Palindrome Number 回文数

    Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...

  4. 蜗牛慢慢爬 LeetCode 9. Palindrome Number [Difficulty: Easy]

    题目 Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could nega ...

  5. LeetCode 9. Palindrome Number (回文数字)

    Determine whether an integer is a palindrome. Do this without extra space. 题目标签:Math 题目给了我们一个int x, ...

  6. Leetcode 9. Palindrome Number(判断回文数字)

    Determine whether an integer is a palindrome. Do this without extra space.(不要使用额外的空间) Some hints: Co ...

  7. [LeetCode][Python]Largest Number

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/largest ...

  8. [LeetCode] 9.Palindrome Number - Swift

    Determine whether an integer is a palindrome. Do this without extra space. 题目意思:判断一个整数是否是回文数 例如:1232 ...

  9. [LeetCode] 9. Palindrome Number 验证回文数字

    Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same back ...

随机推荐

  1. SQL语句执行效率及分析

    查询效率分析:子查询为确保消除重复值,必须为外部查询的每个结果都处理嵌套查询.在这种情况下可以考虑用联接查询来取代.如果要用子查询,那就用EXISTS替代IN.用NOT EXISTS替代NOT IN. ...

  2. PNPOLY - Point Inclusion in Polygon W. Randolph Franklin

    测试目标点是否在多边形内int pnpoly(int nvert, float *vertx, float *verty, float testx, float testy) { int i, j, ...

  3. Spring、编码剖析Spring管理Bean的原理

    引入dom4j jar包 1.新建Person接口和PersonBean public interface PersonIService { public void helloSpring(); } ...

  4. 用Meta 取消流量器缓存实现每次访问都刷新页面方便调试

    如果想禁止浏览器从本地缓存中调阅页面,可以设置网页不保存在缓存中,每次访问都刷新页面,下面是Meta在这方便的用法,需要的朋友可以参考下: <!-- 禁止浏览器从本地缓存中调阅页面.--> ...

  5. MySQL Troubleshoting:Waiting on query cache mutex

    今天被MySQL Query Cache 炕了.线上大量 Waiting on query cache mutex 那么什么是 Query Cache? QC 缓存的是整个SELECT的结果集.而非执 ...

  6. Eclipse快捷键大全(一)

    Eclipse快捷键大全(一) 常用(系统默认): 1.Format (自动排版) : Ctrl+Shift+F 2.Organize Imports (自动导入) : Ctrl+Shift+O 3. ...

  7. [置顶] 浅析objc的消息机制

    学习ios的同学都知道ojbc一种runtime的语言,runtime表明函数的真正执行的时候来确定函数执行的.这样的好处就是我们能很灵活的设计我们的代码,也能在看似合法的情况下做一些非常有意思的事情 ...

  8. EF5 通用数据层 增删改查操作,泛型类(转)

    using System; using System.Collections.Generic; using System.Data.Entity.Infrastructure; using Syste ...

  9. MongoDB无法启动的解决方法

    http://dmyz.org/archives/423 遇到MongoDB突然无法启动,第一反应是删除mongod.lock.这个文件在MongoDB的数据库目录下,默认是/data/db.这是最常 ...

  10. Android开发问题汇总(持续更新)

    在Android开发中,总会有一些很小的问题.由于我们的不仔细,很容易忽略掉,从而导致在该问题上花费了很多的时间,造成工作进度的延迟. 为此,在这里做一下记录,避免再次浪费许多时间在这些问题上. 1. ...