[LeetCode][Python]Palindrome Number
# -*- 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的更多相关文章
- Leetcode练习题 Palindrome Number
9. Palindrome Number Question: Determine whether an integer is a palindrome. An integer is a palindr ...
- Leetcode 9. Palindrome Number(水)
9. Palindrome Number Easy Determine whether an integer is a palindrome. An integer is a palindrome w ...
- leetcode 9 Palindrome Number 回文数
Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...
- 蜗牛慢慢爬 LeetCode 9. Palindrome Number [Difficulty: Easy]
题目 Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could nega ...
- LeetCode 9. Palindrome Number (回文数字)
Determine whether an integer is a palindrome. Do this without extra space. 题目标签:Math 题目给了我们一个int x, ...
- Leetcode 9. Palindrome Number(判断回文数字)
Determine whether an integer is a palindrome. Do this without extra space.(不要使用额外的空间) Some hints: Co ...
- [LeetCode][Python]Largest Number
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/largest ...
- [LeetCode] 9.Palindrome Number - Swift
Determine whether an integer is a palindrome. Do this without extra space. 题目意思:判断一个整数是否是回文数 例如:1232 ...
- [LeetCode] 9. Palindrome Number 验证回文数字
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same back ...
随机推荐
- Javascript 拖拽雏形——逐行分析代码,让你轻松了解拖拽的原理
拖拽的原理: 其实就是鼠标与左上角的距离保持不变.我们来看下图, 这红点就是鼠标. 拖拽拖拽实际上来说就是通过鼠标的位置来计算物体的位置,就是这么简单,就是这么任性. 那这个距离怎么求呢?? 鼠标的位 ...
- Node.js内置的工具和第三方模块来进行单步调试
1.命令行调试: Node.js调试命令: run 执行脚本,在第一行暂停 restart 重新执行脚本 cont,c 继续执行,知道遇到下一个断点 next,n 单步执行 step,s 单步执行,并 ...
- MySQL表结构同步工具 mysql-schema-sync
mysql-schema-sync 是一款使用go开发的.跨平台的.绿色无依赖的 MySQL 表结构自动同步工具.用于将线上(其他环境)数据库结构变化同步到测试(本地)环境! 可以解决多人开发,每人都 ...
- liunx下NetworkManager导致网卡不能启动
前几天在客户现场,配置一台系统为redhat 6.0的服务器,这台服务器是IBM x3755,系统是预装的.在把服务器的IP地址配置完成后,使用命令不能启动网卡.提示:弹出界面 eht0:错误:激活链 ...
- [转]Hibernate中property-ref的应用,常用来解决遗留数据库One To Many关系
[转]Hibernate中property-ref的使用,常用来解决遗留数据库One To Many关系 1.如表Class(ClassID,Class_No,ClassName)与Student(S ...
- BeyondCompare常用功能图解
http://jingyan.baidu.com/article/066074d68f847ec3c31cb05a.html http://lovesoo.org/use-file-compariso ...
- 云服务和虚拟机的预留 IP 地址
大家好! 我很高兴地向大家宣布,云服务和虚拟机的预留 IP 地址将自 2014年 5月 12日起正式发布.在这篇博客中,我们将演示如何管理预留 IP.将预留 IP 与云服务和虚拟机关联.定价模型和一些 ...
- C++多字节字符转换为宽字符的两种方法
目前知道有两种方式:可以提供宽字符与ANSI字符之间的转换, 第一种由COM库提供的函数 char* _com_util::ConvertBSTRToString(BSTR ); BSTR _com ...
- 【HDU】1717 小数化分数2 ——计数原理
小数化分数2 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- Oracle 转换函数
Oracle 转换函数 -- TO_CHAR(date|number {,fmt} {,nlsparams}) fmt:格式内容,返回的字符串是什么格式的,在此处指定:nlsparams:指定国家语言 ...