###solution1####small data
# def reverse(x):
# res=[]
# t=0
# p=1 #记录位数
# y=x
# if x<0:
# x=-x
# while x//10!=0:
# p=p+1
# res.append(x%10)
# x=x//10
# res.append(x)
# l=p
# p=p-1
# for i in range(l):
# t=t+res[i]*(10**p)
# p=p-1
# if y>0:
# return t
# else:
# return -t def reverse(x):
res = 0
if x > 0 and x <= 2 ** 31 - 1:
l = list(str(x))
newl = l[::-1]
res = int(''.join(newl))
if res > 2 ** 31:
return 0
else:
return res
elif x < 0 and x >= - 2 ** 31:
l2 = list(str(abs(x)))
newl2 = l2[::-1]
res = int(''.join(newl2)) * (-1)
if res < -2 ** 31:
return 0
else:
return res
else:
res = 0
if __name__=='__main__':
a=-123
print(reverse(a))

  方法一只适用短整形

  方法二适用长整形

007_Reverse Integer的更多相关文章

  1. LeetCode 7. Reverse Integer

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

  2. Integer.parseInt 引发的血案

    Integer.parseInt 处理一个空字符串, 结果出错了, 程序没有注意到,搞了很久, 引发了血案啊!! 最后,终于 观察到了, 最后的部分: Caused by: java.lang.NoC ...

  3. 由一个多线程共享Integer类变量问题引起的。。。

    最近看到一个多线程面试题,有三个线程分别打印A.B.C,请用多线程编程实现,在屏幕上循环打印10次ABCABC- 看到这个题目,首先想到的是解决方法是定义一个Integer类对象,初始化为0,由3个线 ...

  4. [LeetCode] Integer Replacement 整数替换

    Given a positive integer n and you can do operations as follow: If n is even, replace n with n/2. If ...

  5. [LeetCode] Integer Break 整数拆分

    Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...

  6. [LeetCode] Integer to English Words 整数转为英文单词

    Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...

  7. [LeetCode] Roman to Integer 罗马数字转化成整数

    Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...

  8. [LeetCode] Integer to Roman 整数转化成罗马数字

    Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...

  9. [LeetCode] String to Integer (atoi) 字符串转为整数

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

随机推荐

  1. hdu 1503 Advanced Fruits(LCS输出路径)

    Problem Description The company "21st Century Fruits" has specialized in creating new sort ...

  2. http://bsideup.blogspot.com/2015/04/spring-boot-thrift-part3.html

    Building Microservices with Spring Boot and Apache Thrift. Part 3. Asynchronous services Posted on 4 ...

  3. height、clientHeight、offsetHeight、scrollHeight、height()、 innerHeight()、outerHeight()等的区别

    1.height height是css属性,这个属性定义元素内容区的高度,在内容区外面可以增加内边距.边框和外边距. 当  box-sizing: content-box 时,高度应用到元素的内容框. ...

  4. 第三篇-ubuntu18.04下截图快捷键

    ubuntu自带的截图工具感觉能够满足基本的截图功能,可以不必安装另外的截图软件. 一般用到的截图类型有三种:全屏.当前活动窗口.自定义区域,其中自定义区域截图是最灵活也是我们用的最多的方式.在ubu ...

  5. File类三种得到路径的方法

    转: File类三种得到路径的方法 2010年11月29日 20:37:00 ssyan 阅读数:27123 标签: filemicrosoftstringexceptionwindowsunix   ...

  6. 安装Androidsdudio时

    安装Androidsdudio时,Androidsdudio的路劲可以任意选择,但Androidsdk的路劲就默认到c盘里.然后安装后就可以直接用了

  7. POJ 3463 Sightseeing (次短路经数)

    Sightseeing Time Limit: 2000MS   Memory Limit: 65536K Total Submissions:10005   Accepted: 3523 Descr ...

  8. Elastic 安装篇(1)

    1.Elasticsearch下载安装 https://www.elastic.co/cn/downloads/elasticsearch 解压: 2.安装head https://github.co ...

  9. fork()、vfork()、clone()的区别

    因为生活的复杂,这是一个并行的世界,在同一时刻,会发生很多奇妙的事情,北方下雪,南方下雨,这里在吃饭,那边在睡觉,有人在学习,有人在运动,所以这时一个多彩多姿的世界,每天都发生着很多事情,所以要想很好 ...

  10. Qt ------ excel 操作

    写 excel // step1:连接控件 QAxObject* excel = new QAxObject(this); excel->setControl("Excel.Appli ...