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. ...
随机推荐
- DBA 需要知道N种对数据库性能的监控SQL语句
--DBA 需要知道N种对数据库性能的监控SQL语句 -- IO问题的SQL内部分析 下面的DMV查询可以来检查当前所有的等待累积值. Select wait_type, waiting_tasks_ ...
- Unix 网络编程 I/O 模型 第六章
前提,也是重点是, 当接收收据.或者读取数据时,分两步 1 等待数据准备好. 2 从内核拷贝数据到进程. 对于一个network IO 即 socket(这里我们以read举例),它会涉及到两个系统对 ...
- ubuntu——printk()函数总结,关于日志文件
我们在使用printk()函数中使用日志级别为的是使编程人员在编程过程中自定义地进行信息的输出,更加容易地掌握系统当前的状况. 对程序的调试起到了很重要的作用. (下文中的日志级别和控制台日志控制级别 ...
- Linux程序调试GDB——数据查看
查看栈信息 当程序被停住了,首先要确认的就是程序是在哪儿被断住的.这个一般是通过查看调用栈信息来看的.在gdb中,查看调用栈的命令是backtrace,可以简写为bt. (gdb) bt #0 ...
- HTML-IE6复制BUG
在IE6下使用浮动可能会出现文字重复的情况. 在IE6下,浮动层之间有注释文字的话,之前那个浮动层的内容文字就有可能遭遇一个“隐形”的复制,但是代码里查看文字可并没有多出来. 看个例子: XML/HT ...
- HDU 4946 Area of Mushroom 共线凸包
题意是在二维平面上 给定n个人 每一个人的坐标和移动速度v 若对于某个点,仅仅有 x 能最先到达(即没有人能比x先到这个点或者同一时候到这个点) 则这个点称作被x占有 若有人能占有无穷大的面积 则输出 ...
- DevExpress中GridControl列转义的实现方法
/// <summary> /// CustomColumnDisplayText Helper /// </summary> /// <param name=" ...
- 使用VS2012遇到的问题
问题1:VS2012 编译程序时:无法查找或打开PDB文件 解决方法:调试-选项-符号-Microsoft符号服务器打钩,然后确定,就OK了. 问题2:按F5运行.c程序,dos窗口闪退 解决方法:C ...
- id ,NSObject, id<NSObject>区别
转自:http://blog.csdn.net/happytengfei/article/details/11473931 我们经常会混淆以下三种申明(我是没有留意过): 1. id foo1; ...
- 每日英语:The First Day On A Job Is Tough Work
Why is the first day on the job often the worst? New employees tend to be greeted with stacks of ben ...