# -*- 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. PHP框架学习之Laravel安装

    自从接触PHP以来一直使用Yii,感觉Yii实现功能比较简单,是一个很不错的框架.最近由于工作的原因开始研究Laravel5,不得不说我在第一步安装就被坑着了,下面就是我痛苦的学习成果.Laravel ...

  2. Jquery 解决 H5 placeholder元素问题

    <style type="text/css"> .placeholder{ color: #cacaca; } </style> <script ty ...

  3. 汽车总线obd模拟器,obd仿真器,ecu模拟器,obd开发测试工具,可以模拟ecu中的obd协议 MRD-5050

    汽车总线OBD模拟器MRD-5050型号是在车辆越来越趋于网络化的趋势下研发的,是汽车产品开发.调试.生产必备的工具,能为为开发人员节省大量的时间.当前车辆上的总线设备越来越多,有的高端车上甚至多到有 ...

  4. CC++初学者编程教程(9) Windows8.1安装VS2013并捆绑QT与编程助手

    我们在Windows8.1安装VS2013并捆绑QT与编程助手需要下列文件. 2. 在虚拟机中开启Windows8.1 3.然后选择VS2013的安装镜像. 4.将镜像复制到虚拟机. 5.我们装载这个 ...

  5. 关于chrome浏览器更新后快捷工具失效的解决方法

    更新chrome浏览器到29.0时发现快捷工具的双击关闭标签页失效了,解决办法: 1.打开chrome的文件夹.C:\Program Files (x86)\Google\Chrome\Applica ...

  6. Android SoundPool 的使用以及原理分析

    好吧,我们今天来聊聊SoundPool这东西. 据说这个东西是冰激凌(Android4.0)里才引入的一个新东西.按照官方的意思大多数情况下是给游戏开发用的,比如一个游戏10关,它能在游戏开始前一次加 ...

  7. 【巧妙思维】【4-6】Problem F

    题意:有n个正方体,边长为A[i] 当A[k]-A[p]<=lim 时 k可以放在p上面, 问有多少种放法: 一开始被数据范围吓到了 ,以为是n^3算法,答案是nlogn 从小到大排序,一个一个 ...

  8. DEV控件之ChartControl用法

    一.总体概述 这个控件包含3层,最外面的chartControl层.中间的XYDiagram层.最里面的Series层.功能非常强大,但同时使用起来也相对复杂,需要各个层之间相互协调设置才能达到自己想 ...

  9. 得到文件的MD5值

    /// <summary> /// 得到文件的MD5值 /// </summary> /// <param name="Path">文件路径&l ...

  10. CentOS7与Win7双系统引导问题

    先安装的Win7,后安装的CentOS7,结果系统引导就只有CentOS7了.记得以前CentOS6.x系列没这个问题,主要是由于CentOS7.x使用grub2的原因吧. 方案一:使用Win PE. ...