mycode

class Solution(object):
def reverse(self, x):
"""
:type x: int
:rtype: int
"""
a = str(x)
if a[0] == '-':
flag = '-'
a = a[1:]
else :
flag = ''
a = a[::-1]
while True:
if a[0] == '':
if len(a) > 1: a = a[1:]
else: return 0
else:
a = int(flag+a)
if a > 2147483647 :
return 0
elif a < -2147483648 :
return 0
else:
return a

注意:用int(x)时,会自动把x前面的0去掉

class Solution(object):
def reverse(self, x):
"""
:type x: int
:rtype: int
"""
a = str(x)
if a[0] == '-':
flag = '-'
a = a[1:]
else :
flag = ''
a = a[::-1]
#while True:
# if a[0] == '0':
# if len(a) > 1: a = a[1:]
# else: return 0
# else:
a = int(flag+a)
if a > 2147483647 :
return 0
elif a < -2147483648 :
return 0
else:
return a

参考

class Solution(object):
def reverse(self, x):
"""
:type x: int
:rtype: int
"""
x = int(str(x)[::-1]) if x >= 0 else - int(str(-x)[::-1])
return x if x < 2147483648 and x >= -2147483648 else 0

  

leetcode-easy-string-7 Reverse Integer的更多相关文章

  1. Leetcode 题目整理-2 Reverse Integer && String to Integer

    今天的两道题关于基本数据类型的探讨,估计也是要考虑各种情况,要细致学习 7. Reverse Integer Reverse digits of an integer. Example1: x = 1 ...

  2. LeetCode 【2】 Reverse Integer --007

    六月箴言 万物之中,希望最美:最美之物,永不凋零.—— 斯蒂芬·金 第二周算法记录 007 -- Reverse Integer (整数反转) 题干英文版: Given a 32-bit signed ...

  3. leetcode第七题Reverse Integer (java)

    Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, retu ...

  4. LeetCode之“数学”:Reverse Integer && Reverse Bits

    1. Reverse Integer 题目链接 题目要求: Reverse digits of an integer. Example1: x = 123, return 321 Example2:  ...

  5. leetCode练题——7. Reverse Integer

    1.题目:   7. Reverse Integer Given a 32-bit signed integer, reverse digits of an integer. Example 1: I ...

  6. 【LeetCode】7 & 8 - Reverse Integer & String to Integer (atoi)

    7 - Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Notic ...

  7. 【LeetCode】7、Reverse Integer(整数反转)

    题目等级:Easy 题目描述: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 O ...

  8. leetcode第七题--Reverse Integer

    Problem: Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 ...

  9. 【LeetCode算法-7】Reverse Integer

    LeetCode第7题: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Outp ...

  10. LeetCode记录之7——Reverse Integer

    Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Note:The ...

随机推荐

  1. 误删除/dec/zero,/dev/null

    误删除/dev/zero [root@MYSQL-MONGO145 dev]# mknod /dev/zero c 1 5[root@MYSQL-MONGO145 dev]# chmod 666 /d ...

  2. js获取7天之前的日期或者7天之后的日期

    js获取7天之前的日期或者7天之后的日期(网上摘取的,记录自己使用) function fun_date(num) { var date1 = new Date(); //今天时间 var time1 ...

  3. python 目录管理与文件管理

    目录管理(os) system:执行系统命令 # 执行系统命令 os.system('cls') name:获取操作系统名称 # 操作系统名称,nt代表Windows, posix代表类unix pr ...

  4. react 不同环境配置不同域名

    npm eject 先将配置文件暴露出来 将scripts中的build文件复制一份,改名为你需要的名字 将其中的 process.env.NODE_ENV 赋值为你需要的环境 在package.js ...

  5. CentOS7搭建FastDFS V5.11分布式文件系统(二)

    1.CentOS7 FastDFS搭建 前面已下载好了要用到的工具集,下面就可以开始安装了: 如果安装过程中出现问题,可以下载我提供的,当前测试可以通过的工具包: 点这里点这里 1.1 安装libfa ...

  6. Js 将图片的绝对路径转换为base64编码

    转.... 我们可以使用canvas.toDataURL的方法将图片的绝对路径转换为base64编码:在这我们引用的是淘宝首页一张图片如下:  var img = "https://img. ...

  7. 5.(基础)tornado异步

    终于到了传说中的异步了,感觉异步这个名字听起来就很酷酷的,以前还不是多擅长Python时,就跑去看twisted的源码,结果给我幼小的心灵留下了创伤.反正包括我在内,都知道异步编程很强大,但是却很少在 ...

  8. Centos7 下Yum安装OpenLdap

    网上的教程一大堆,也没用具体说明版本,所以很多操作方法都不一样,把我踩过的坑记录下来 环境: Centos7 OpenLdap 2.4.44 openldap新版本和老版本的配置方法差别特别大 安装步 ...

  9. 爬虫相关基础技术铺垫---多线程Thread和队列Queue应用

    from queue import Queue from threading import Thread class mydownloader(Thread): def __init__(self,q ...

  10. 【LuoguP5383】[模板]普通多项式转下降幂多项式

    传送门 Sol (怎么老是有人喜欢出新的多项式毒瘤板子,懒得整到一起了) 核心就是把 幂用下降幂来代替. 使用斯特林数展开幂为下降幂: \[x^n=\sum_{i=0}^n{x\choose i}i! ...