【leetcode❤python】 9. Palindrome Number
#回文数
#Method1:将整数转置和原数比较,一样就是回文数;负数不是回文数
#这里反转整数时不需要考虑溢出,但不代表如果是C/C++等语言也不需要考虑
class Solution(object):
def isPalindrome(self, x):
"""
:type x: int
:rtype: bool
"""
if x<0:return False
#负数不是回文数,return False
xre=x
ans=0
while x>0:
ans=ans*10+x%10
x=x//10
if ans>21474836547:
ans=0
print ans ,xre
return ans==xre
#Method2:不反转整数,将数字逐个分离,比较最前与最后是否一样
class Solution(object):
def isPalindrome(self, x):
"""
:type x: int
:rtype: bool
"""
if x<0:return False
#负数不是回文数,return False
digits=1
while x/digits>=10:
digits*=10
while digits>1:
right=x%10
left=x/digits
if left!=right:return False
x=(x%digits)/10
digits/=100
return True
【leetcode❤python】 9. Palindrome Number的更多相关文章
- 【leetcode❤python】 374. Guess Number Higher or Lower
#-*- coding: UTF-8 -*-# The guess API is already defined for you.# @param num, your guess# @return - ...
- 【leetcode❤python】 234. Palindrome Linked List
#-*- coding: UTF-8 -*-class Solution(object): def isPalindrome(self, head): ""&q ...
- 【leetcode❤python】Convert a Number to Hexadecimal
#-*- coding: UTF-8 -*- class Solution(object): hexDic={0:'0',1:'1',2:'2',3:'3',4:'4',5:'5',6:'6', ...
- 【leetcode❤python】263. Ugly Number
class Solution(object): def isUgly(self, num): if num<=0:return False comlist=[2 ...
- 【leetcode❤python】Sum Of Two Number
#-*- coding: UTF-8 -*- #既然不能使用加法和减法,那么就用位操作.下面以计算5+4的例子说明如何用位操作实现加法:#1. 用二进制表示两个加数,a=5=0101,b=4=0100 ...
- 【leetcode❤python】 125. Valid Palindrome
#-*- coding: UTF-8 -*- class Solution(object): def isPalindrome(self, s): ""&quo ...
- 【leetcode❤python】 414. Third Maximum Number
#-*- coding: UTF-8 -*- #l1 = ['1','3','2','3','2','1','1']#l2 = sorted(sorted(set(l1),key=l1.index,r ...
- 【leetcode❤python】409. Longest Palindrome
#-*- coding: UTF-8 -*- from collections import Counterclass Solution(object): def longestPalindro ...
- 【leetcode❤python】191. Number of 1 Bits
#-*- coding: UTF-8 -*- class Solution(object): def hammingWeight(self, n): if n<=0:retu ...
随机推荐
- linux时区的设置
到目前为止,个人的理解就是linux中设置时区就是修改配置文件 /etc/localtime 而通常的做法就是让这个文件作为符号链接,链接到 /usr/share/zoneinfo/ 中的某个特定的时 ...
- [sinatra] Just Do It: Learn Sinatra, Part One Darren Jones
1. Install sinatra gem gem install sinatra --no-ri --no-rdoc 2. Basic App #!/usr/bin/ruby require 's ...
- dbo与db_owner区别
dbo 是具有在数据库中执行所有活动的暗示性权限的用户.将固定服务器角色 sysadmin 的任何成员都映射到每个数据库内称为 dbo 的一个特殊用户上.另外,由固定服务器角色 sysadmin 的任 ...
- spring的定时任务
maven中引入quartz的jar包依赖 单纯针对时间的设置规则org.springframework.scheduling.quartz.CronTriggerBean允许你更精确地控制任务的运行 ...
- html规范总结
这个链接有规范的html 描述:http://nec.netease.com/standard 相关链接: 1. http://www.zhangxinxu.com/wordpress/2010/09 ...
- 关闭 Windows 的常用端口
netstat -ano 可以看到目前开着哪些端口 netstat -ano|findstr <端口号> 可以找到开放的端口的那条,最后还列出了 PID. 然后到任务管理器中,你可以查 ...
- 某硕笔试题mysql数据库部分(较为全面)
Student(S#,Sname,Sage,Ssex) 学生表 Course(C#,Cname,T#) 课程表 SC(S#,C#,score) 成绩表 Teacher(T#,Tname) 教师表 ...
- Creater中选择一行的方法
1. 在表布局中增加一单选钮列,给单选钮的属性name任意设定一个值.2. 选择单选钮对应列,将其selectID设为单选钮的ID;将onclick设为setTimeout('initAllRow ...
- ubunu下用命令设置壁纸
ubunu下用命令设置壁纸: gsettings set org.gnome.desktop.background picture-uri “file:[fileName]” eg:gsettings ...
- linux中的优先搜索树的实现--prio_tree【转】
转自:http://blog.csdn.net/bailyzheng/article/details/8041943 linux中的优先搜索树的实现--prio_tree prio_tree在linu ...