007_Reverse Integer

###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的更多相关文章
- LeetCode 7. Reverse Integer
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have you ...
- Integer.parseInt 引发的血案
Integer.parseInt 处理一个空字符串, 结果出错了, 程序没有注意到,搞了很久, 引发了血案啊!! 最后,终于 观察到了, 最后的部分: Caused by: java.lang.NoC ...
- 由一个多线程共享Integer类变量问题引起的。。。
最近看到一个多线程面试题,有三个线程分别打印A.B.C,请用多线程编程实现,在屏幕上循环打印10次ABCABC- 看到这个题目,首先想到的是解决方法是定义一个Integer类对象,初始化为0,由3个线 ...
- [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 ...
- [LeetCode] Integer Break 整数拆分
Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...
- [LeetCode] Integer to English Words 整数转为英文单词
Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...
- [LeetCode] Roman to Integer 罗马数字转化成整数
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...
- [LeetCode] Integer to Roman 整数转化成罗马数字
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
- [LeetCode] String to Integer (atoi) 字符串转为整数
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
随机推荐
- JDK8中的并行流
1.IntStream.parallel():获取并行流处理 2. Collection中调用parallelStream()获取并行流 3.并行排序Arrays.parallelSort()
- oh-my-zsh: bracketed-paste-magic:zle:47: not enough arguments for -U
今天突然发现粘贴的时候就提示这个错误,通过查阅资料已经解决 解决方法 分析问题,是oh-my-zsh的bracketed-paste-magic功能出现了异常,并未找到更好的方法,暂时先禁用掉. 修改 ...
- bzoj4566 找相同字符
题意:给定两个字符串,从中各取一个子串使之相同,有多少种取法.允许本质相同. 解:建立广义后缀自动机,对于每个串,分别统计cnt,之后每个点的cnt乘起来.记得开long long #include ...
- “adb不是内部或外部命令,也不是可运行的程序或批量文件“
首先这个问题有两种可能: 1.就是没有配置环境变量, 这个只需要将android安装:例如C:\Program File\android-sdk-windows\tools加入到 系统变量Path中, ...
- script id
Script中的id还是有用的,比如如果页面需要加载的JS文件过多,那样最好是写一个JS文件用来加载这些JS文件 require: function(libraryName){ document.wr ...
- Mac idea中git igenore设置
- 完美解决windows+ngnix+phpcgi自动退出的问题
[摘要]在windows下搭建nginx+php环境时,php-cgi.exe会经常性的自动关闭退出,本文介绍通过使用xxfpm进程管理器管理php-cgi.exe. php-cgi.exe在wind ...
- PHP-FIG - PHP 标准规范
转自:https://psr.phphub.org/ PHP 标准规范 PSR 是 PHP Standard Recommendations 的简写,由 PHP FIG 组织制定的 PHP 规范,是 ...
- 递归思维判断数组a[N]是否为一个递增数组
递归的方法:记录当前最大的,并且判断当前的是否比这个还大,大则继续,否则返回false结束: bool fun( int a[], int n ) { ) { return true; } ) { ] ...
- asp一句话
<%eval""&("e"&"v"&"a"&"l"&& ...