[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 ...
随机推荐
- python基础(四)
内置函数 callable() #判断是否能够被调用执行,可以调用返回True,例如函数和类 chr() #将ascii值转换为字符,例如print(chr(65) )输出为a ord()#将字符转换 ...
- Spring摘记
spring工作机制及为什么要用? 1.spring mvc请所有的请求都提交给DispatcherServlet,它会委托应用系统的其他模块负责负责对请求进行真正的处理工作.2.Dispatcher ...
- 5路数字量输入Di,5路大电流继电器输出,可电脑控制,支持modbus协议工业模块,支持和DCS,PLC无缝对接。
数字量输入输出模块MRD-5060具有5路DI,5路继电器输出,1路485接口(支持Modbus RTU),能实现5路DI(干接点输入)转485的采集,也可以通过485控制5路继电器(5A30VDC, ...
- CC EAL认证
国际通用准则(CC) CC(Common Criteria)是国际标准化组织统一现有多种准则的结果,是目前最全面的评价准则.1996年6月,CC第一版发布:1998年5月,CC第二版发布:1999年 ...
- 假数据自我添加测试--NSArray object
一.列表假数据 //在.h文件里面定义node所包含实体类---1 struct listTestNode { NSString *image; NSString *name; }; //在.m实现文 ...
- C语言的本质(5)——类型转换的本质与处理
数据类型转换的方式 C 语言中的数据类型转换可分为隐式转换和显式转换两种. 隐式转换 隐式转换也可称作为自动转换,它经常以以下几种形式发生: 1.在计算一个同时出现多种数据类型的表达式时,将所有数据类 ...
- SP_CreateInsertScript 将表内的数据全部拼接成INSERT字符串输出
),)) as begin set nocount on ) ) ) select @sqlstr='select ''insert '+@tablename select @sqlstr1='' s ...
- poj1484---判断保险丝是否烧断
题目输入要求: 2 2 10 //设备数n 接下来的操作数m 保险丝能承受最大电流c5 //电器1的电流7 //2的电流1 //反转开关12 //反转开关2 思路:设置一个flag数组,记得每次 ...
- poj 3250 Bad Hair Day(单调队列)
题目链接:http://poj.org/problem?id=3250 思路分析:题目要求求每头牛看见的牛的数量之和,即求每头牛被看见的次数和:现在要求如何求出每头牛被看见的次数? 考虑到对于某头特定 ...
- wxpython 中的所有控件及高级应用
转自http://xoomer.virgilio.it/infinity77/Phoenix/lib.agw.html,,,哈哈终于找到了这块的内容,书上基本没有讲解 This is the Adva ...