###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. redis 从入门到遗忘

    Key操作 keys * *: 通配任意多个字符 ?: 通配单个字符 []: 通配括号内的某1个字符 exists key 存在返回1,不存在返回0 type key rename oldkey ne ...

  2. hex文件格式

    hex文件格式是可以烧写到单片机中,被单片机执行的一种文件格式,生成Hex文件的方式有很多种,可以通过不同的编译器将C程序或者汇编程序编译生成hex.   Hex文件格式解析 Hex文件如果用特殊的程 ...

  3. hdu 3790 最短路问题 (spfa练手)

    Problem Description 给你n个点,m条无向边,每条边都有长度d和花费p,给你起点s终点t,要求输出起点到终点的最短距离及其花费,如果最短距离有多条路线,则输出花费最少的.   Inp ...

  4. python 类的介绍

    使用面向对象的优点: 1.能够更好的设计软件架构 2.维护软件模块 3.易于架构和组件的重用 类的定义: 构造函数:初始化用,写不写都可以,默认为空 类属性:属于类的对象 方法属性:不属于类的对象 私 ...

  5. Percona xtrabackup

    关于percona xtrabackup     percona xtrabackup是世界上唯一开源的.免费的MySQL热备份软件,可以为InnoDB存储引擎和XtraDB存储引擎进行不间断的备份, ...

  6. Luogu P3966 [TJOI2013]单词

    题目链接 \(Click\) \(Here\) 本题\(AC\)自动机写法的正解之一是\(Fail\)树上跑\(DP\). \(AC\)自动机是\(Trie\)树和\(Fail\)树共存的结构,前者可 ...

  7. Ajxa用在哪些方面(六)

    Ajax只有在适当的地方才会用到,也即需要在后台进行大量数据处理(充当消息通知机制),需要频繁的定时或不定时从服务器中读取数据的时候.1.比如我们常接触电子邮件系统中,因为电子邮件系统一般都由电子邮件 ...

  8. 【知名的3D造型设计软件】犀牛 Rhinoceros 5.5.2 for Mac

    [简介] 今天和大家分享最新的3D设计软件 犀牛 Rhinoceros for Mac 5.5.2 版本,支持中文界面,这是一款Mac上知名的3D造型软件,犀牛可以广泛地应用于三维动画制作.工业制造. ...

  9. python 微信爬虫实例

    单线程版: import urllib.request import urllib.parse import urllib.error import re,time headers = (" ...

  10. OS + Linux nmon / nmon analyser / nmon_analyser_v52_1.zip

    s nmon_analyser_v52_1.zip https://www.ibm.com/developerworks/community/wikis/home?lang=en#!/wiki/Pow ...