###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主从复制与高可用方案

    redis简单介绍 Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库.Redis与其他key – value缓存产品有以下三个特点: 支持数据的持久化,可以将内存中 ...

  2. Java collection 容器

    http://www.codeceo.com/article/java-container-brief-introduction.html Java实用类库提供了一套相当完整的容器来帮助我们解决很多具 ...

  3. 【洛谷P4318】完全平方数

    题目大意:求第 K 个无平方因子数. 题解:第 k 小/大的问题一般采用二分的方式,通过判定从 1 到当前数中满足某一条件的数有多少个来进行对上下边界的转移. 考虑莫比乌斯函数的定义,根据函数值将整数 ...

  4. 编译 pcre - 开源的正则表达式(库)

    PCRE百科介绍: PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库.这些在执行正规表达式模式匹配时用与Perl ...

  5. spring+shiro共享session完整小例子

    之前写过一个,只不过那个不单纯,有人跑不通,所以今天整个纯粹的小例子. 要求你有Redis. 源码 GitHub 目录结构 因为这是个例子,仅仅为了体现共享session,所以权限认证部分没有加入处理 ...

  6. HR算法具体过程

    首先研究HR算法在概率分布估计中的实现,我们再考虑如何将其应用于频繁项挖掘中. 一.确定输入数据类型 def generate_uniform_distribution(k): raw_distrib ...

  7. centos7 mysql-server 安装过程

    官网下载安装mysql-server # wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm # rpm -i ...

  8. ajax请求封装函数

    写封装函数的套路 1.先写出这个函数原来的基本用法 2.写一个没有形参空函数,将上一步的代码直接作为函数体, 3.根据使用过程中,抽象出来需要变的东西作为形参 function ajax (metho ...

  9. qml:: QVariant转为自定义类型

    QVariant可以实现C++与qml之间的自定义类型的传递: 以QObject类型为例: 1.  QObject转为QVariant QVariant var = QVariant::fromVal ...

  10. saltstack syndic

    #syndic 相当于master的代理,master通过syndic代理控制node主机 master <------ syndic+master <---------- node ma ...