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. ...
随机推荐
- vue 1.x 总结
1.Vuejs组件 vuejs构建组件使用 Vue.component('componentName',{ /*component*/ }): 这里注意一点,组件要先注册再使用,也就是说: Vue.c ...
- Java之进阶(1) -《Effective Java》
第1章 引言 第2章 创建和销毁对象 第1条:考虑用静态工厂方法代替构造器(Consider static factory methods instead of constructors) 第2条:遇 ...
- PHP-php.ini中文版
今天细看了下配置文件 有很多没用过的 就从网上搜了一篇 常看看 ;;;;;;;;;;;;;;;; 简介 ;;;;;;;;;;;;;;;;; 本文并非是对英文版 php.ini 的简单翻译,而是参考了众 ...
- Linux命令-文件搜索命令:which
主要用途:查找linu命令,而不是磁盘上的普通文件,并且能看到命令的别名和目录. 区别whereis命令,which在path变量指定的目录中查找命令,并且返回第一个符合的结果.whereis是查找所 ...
- scrapy添加 请求头
直接在 setting 文件中添加
- 利用国内的源安装 Python第三方库
我们需要安装一些Python的第三方库,但是使用 pip install + 第三方库 的时候,会出现下载速度慢的问题,当然我们也可以使用国内的源安装. 例如: sudo pip install ...
- unity, destroy gameObject & destroy all children
一,destroy gameObject 删除名为xxx的gameObject 错误方法1: Destroy(xxx); 以上方法之所以错误,是因为Destroy在下一帧才生效,而在本帧之内xxx还存 ...
- linode使用apt更新时停止的错误
使用ubuntu在更新系统时候,有时候会出现这样的问题: 0% [Connecting to security.ubuntu.com (2001:67c:1560:8001::14)] 问了下客户,他 ...
- RVM(ruby version manage)安装指南
一.安装需要3步gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 \c ...
- Atitit.数据库事务隔离级别 attilax 总结
Atitit.数据库事务隔离级别 1. 事务隔离级别的作用 1 2. 在的隔离级别 2 3. 常见数据库的默认管理级别 3 1. 事务隔离级别的作用 较低的隔离级别可以增强许多用户同时访问数据的能力, ...