Leetcode_num4_Reverse Integer
题目:
Reverse digits of an integer.
Have you thought about this?
Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!
If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100.
Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases?
Throw an exception?
Good, but what if throwing an exception is not an option? You would then have to re-design the function (ie, add an extra parameter).
每次一遇到关于整型数的题就有些发怵,看来日后还得加强一下~
此题的难点主要在几个可能出现的bug上。如数字末尾带0的要去0再反转,反转后数值溢出的问题(int类型32位。数值范围在-2^31~2^31之间)
思路分为:
1 对数字去除末尾的零
2 将数字转化为字符串处理,因为字符串的不可变性,採用字符串数组存储反转字符串结果
3 通过比較原数字字符串与最大数值‘2147483647’每一位上数字的大小来解决溢出问题
4 将字符串数组中的数字字符相加,转化成为int类型输出
代码例如以下:
class Solution:
# @return an integer
def reverse(self, x):
if x==0:
return 0
a=0 #dealing with last 0s
b=0
while(b==0):
a=x//10
b=x%10
x=a
x=a*10+b c=str(x)#convert int into string
L=len(c)
s=['' for i in range(L)] MAX='2147483647'#handling overflow case
if c.startswith('-'):
l=len(c)
if l>11:
return 0
elif l==11:
for i in range(1,11):
if c[L-i-1]>MAX[i]:
return 0
else:
l=len(s)
if l>10:
return 0
elif l==10:
for i in range(0,10):
if c[L-i-1]>MAX[i]:
return 0 if c.startswith('-'):
s[0]='-'
for i in range(1,L/2+1):
t=c[i]
s[i]=c[L-i]#it's very crucial
s[L-i]=t
else:
for i in range(0,L/2+1):
t=c[i]
s[i]=c[L-i-1]
s[L-i-1]=t
rs=''
for i in s:
rs+=i
return int(rs)
Leetcode_num4_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. ...
随机推荐
- QtGui.QFontDialog
The QtGui.QFontDialog is a dialog widget for selecting a font. #!/usr/bin/python # -*- coding: utf-8 ...
- laravel 安装环境安了三天!!
各种报错,各种升级,各种重装,重启!! 记录一下一些错误吧,,, 错误太复杂,,,, 原因:版本问题!.CPU虚拟化问题.win10问题.软件兼容性问题.还有就是各种不细心啥的 分割线 ...
- volist 自增序号 分页如何实现?
TP框架模板中如何生成自增数据 {$_GET['p']*10-10+$i} /* 分页序号计算 */ function addnum($k,$num){ return ($k +1 ) ...
- Android 常用的性能分析工具详解:GPU呈现模式, TraceView, Systrace, HirearchyViewer(转)
此篇将重点介绍几种常用的Android性能分析工具: 一.Logcat 日志 选取Tag=ActivityManager,可以粗略地知道界面Displaying的时间消耗.当我们打开一个Activit ...
- 【BIRT】02_开发一张简单的报表
上一节我们已经将开发环境准备完毕,那么接下来就开发一张简单的报表 1.BIRT开发环境 打开已经安装好的BIRT开发环境 1.1新建Project 菜单栏>> file >> ...
- HTML5学习笔记 Geolocation(地理定位)
HTML5 Geolocation(地理定位)用于定位用户的位置. 定位用户的位置 html5 Geolocation API用于获得用户的地理位置 鉴于该特性可能低侵犯用户的隐私,除非用户同意,否则 ...
- 转 多线程 闭锁(Latch) 栅栏(CyclicBarrier)
java多线程并发系列之闭锁(Latch)和栅栏(CyclicBarrier) 标签: java并发编程 2015-05-28 16:45 2939人阅读 评论(0) 收藏 举报 本文章已收录于: . ...
- Python 实int型和list相互转换 现把float型列表转换为int型列表 把列表中的数字由float转换为int型
第一种方法:使用map方法 >>> list = [, ] #带有float型的列表 >>> int_list = map(int,list) #使用map转换 & ...
- jqury插件编写
sae中短信验证码: ; (function($) { $(document).ready(function() { if (parent && parent.location.hre ...
- The Report Of Twisted’s Death or: Why Twisted and Tornado Are Relevant In The Asyncio Age
Speech on PyCon2016 https://www.youtube.com/watch?v=82vuCZ4FLFE