【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 ...
随机推荐
- Unable to find the wrapper "https"错误的解决办法
PHP.ini默认配置下,用file_get_contents读取https的链接,就会如下错误:Warning: fopen() [function.fopen]: Unable to find t ...
- 夺命雷公狗---微信开发13----获取access_token
获得Access Token的方法1: 这里可以手动进行修改: https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential ...
- angularjs 相关资料
1.angularjs 框架结构 bootstrap process :http://www.oschina.net/translate/angularjs-the-next-big-thing?pr ...
- 作为一个web开发人员,哪些技术细节是在发布站点前你需要考虑到的
前日在cnblogs上看到一遍文章<每个程序员都必读的12篇文章>,其中大多数是E文的. 先译其中一篇web相关的”每个程序员必知之WEB开发”. 原文: http://programme ...
- [转]编译Android源代码常见错误解决办法
1. 编译时出现/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.4.5/../../../libz.so when ...
- PHP人民币金额数字转中文大写的函数
<?php header("Content-Type:text/html;charset=utf-8"); error_reporting(2); function cny( ...
- Mac Book 上安装Windows 8 / 10 以后安装 Hyper-v 无法正常使用问题---虚拟化已禁止问题
Mac book由于工作需要装了双启动,平时工作用 Windows 10, 由于有些老程序无法启动,还得再装一个虚拟机. 起初装 WMWare 开始装 7.1版本,结果没安装完就直接崩溃重启, 网上反 ...
- Hibernate,JPA注解@DynamicInsert和@DynamicUpdate,Hibernate如何插入sysdate
@DynamicInsert属性:设置为true,设置为true,表示insert对象的时候,生成动态的insert语句,如果这个字段的值是null就不会加入到insert语句当中.默认false. ...
- strong和copy的区别
问题描述 在定义一个类的property时候,为property选择strong还是copy特别注意和研究明白的,如果property是NSString或者NSArray及其子类的时候,最好选择使用c ...
- tomcat 启动时参数设置说明
使用Intellij idea 其发动tomcat时会配置启动vm options :-Xms128m -Xmx768m -XX:PermSize=64M -XX:MaxPermSize=512m. ...