###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. Oracle数据库中遇到的坑

    最近在帮别人忙写程序,用的是Oracle数据库,写一篇文章来说说在Oracle中遇到的一些坑: 1. PL/SQL develop的坑: 由于在这里工作环境是内网完全,无奈只能使用PL/SQL 工具, ...

  2. webpack入门(五)webpack CLI

    webpack的CLI安装和命令 Installation $ npm install webpack -g The webpack command is now available globally ...

  3. Spring Boot + Mybatis + Redis二级缓存开发指南

    Spring Boot + Mybatis + Redis二级缓存开发指南 背景 Spring-Boot因其提供了各种开箱即用的插件,使得它成为了当今最为主流的Java Web开发框架之一.Mybat ...

  4. A1114. Family Property

    This time, you are supposed to help us collect the data for family-owned property. Given each person ...

  5. 关于TSql

    1.Sql:结构化查询语言(Structrued  Query  Language) 2.TSql:是Sql语言的另一种版本,且只能在SqlServer中使用.和Sql不同的是,TSql中增加了对变量 ...

  6. Day21--Python--C3算法和super()

    一. python的继承. 多继承 子类继承父类. 为什么要继承???? 为了节省开发时间. 调高开发效率. 代码得到了重用 在python中存在多继承 MRO(method resolution O ...

  7. TestNg 9. 参数化测试-DataProvider参数化

    首先利用@DataProvider(name = "XXX")的属性,将name的值XXX 传递给 @Test(dataProvider = "XXX") 看以 ...

  8. bzoj 4326: NOIP2015 运输计划(二分+树链剖分)

    传送门 题解: 树链剖分快速求解任意两点间的路径的权值和: 然后,二分答案: 此题的难点是如何快速求解重合路径? 差分数组可以否??? 在此之前先介绍一下相关变量: int fa[maxn]; int ...

  9. linux基本

    一.初识 Linux与windows相比的优点是:长期稳定的运行,避免了因为系统的问题导致的项目运行中断:占用资源少:开源软件多. Centos(Community Enterprise Operat ...

  10. ansible小结常用模块

    根据官方的分类,将模块按功能分类为:云模块.命令模块.数据库模块.文件模块.资产模块.消息模块.监控模块.网络模块.通知模块.包管理模块.源码控制模块.系统模块.单元模块.web设施模块.window ...