Palindrome Number (python)
2016-07-04 15:24
453 查看
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.
选择分离数字的方法,当然要用到大量除10和取余数的计算。
class Solution(object):
def isPalindrome(self, x):
"""
:type x: int
:rtype: bool
"""
o=x
ret=0
flag=1
if x<0:
return False
while(x!=0):
ret=ret*10+x%10
x=x/10
return ret==o
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.
选择分离数字的方法,当然要用到大量除10和取余数的计算。
class Solution(object):
def isPalindrome(self, x):
"""
:type x: int
:rtype: bool
"""
o=x
ret=0
flag=1
if x<0:
return False
while(x!=0):
ret=ret*10+x%10
x=x/10
return ret==o
相关文章推荐
- Flask项目和文档集
- 利用Python实现归并排序
- Python脚本实现查找指定目录下包含指定关键字的文件
- Python 语言及其应用 Chapter_5_Note_4 Counter()计数器
- python中几种不同类型的参数的区分及其定义时的顺序
- Python 语言及其应用 Chapter_5_Note_3 defaultdict_setdefault
- String to Integer (atoi)(python)
- python的strip()函数使用
- Python菜鸟之路:Python基础-Socket基础-1
- Python Socket 编程,OSError: [Errno 98] Address already in use
- python 的公有和“私有”
- windos7中配置python的环境变量
- 使用sched库完成周期定时任务
- Python读取文件小结(csv,txt)
- 使用Python学习selenium测试工具-6:同步
- python INFO: Can't locate Tcl/Tk libs and/or headers
- 使用Python学习selenium测试工具-5:元素交互
- python深浅拷贝探究
- Json概述以及python对json的相关操作
- python中字符串的使用方法