题目:

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的更多相关文章

  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. Drag & drop a button widget

    In the following example, we will demonstrate how to drag & drop a button widget. #!/usr/bin/pyt ...

  2. MIME protocol 说明

    按照 <张孝祥Java邮件开发详解> 自己create了 emali, 其中jpg 和wav文件格式过大,删除了写内容 From redhat@diego.com Mon Nov 17 0 ...

  3. javascript 创建对象的几种方式

    1. //基于已有对象扩充其属性和方法var object = new Object(); object.name = "zhangsan"; object.sayName = f ...

  4. Android 应用开发实例之文件管理器

    2013-07-02 10.2 文件管理器 能够浏览和管理手机/存储卡上的文件和文件夹,包括重命名.删除.新建.复制.粘帖等文件操作. 由于需要浏览大量的文件/文件夹,所以需要使用一个ListView ...

  5. mysql索引二

    理解MySQL——索引与优化 写在前面:索引对查询的速度有着至关重要的影响,理解索引也是进行数据库性能调优 的起点.考虑如下情况,假设数据库中一个表有10^6条记录,DBMS的页面大小为4K,并存储1 ...

  6. TensorFlow 简单实例

    TF 手写体识别简单实例: TensorFlow很适合用来进行大规模的数值计算,其中也包括实现和训练深度神经网络模型.下面将介绍TensorFlow中模型的基本组成部分,同时将构建一个CNN模型来对M ...

  7. AutoFac文档12(转载)

    目录 开始 Registering components 控制范围和生命周期 用模块结构化Autofac xml配置 与.net集成 深入理解Autofac 指导 关于 词汇表 Resolve的参数 ...

  8. AutoFac文档6(转载)

    目录 开始 Registering components 控制作用域和生命周期 用模块结构化Autofac xml配置 与.net集成 深入理解Autofac 指导 关于 词汇表 实例生命周期 实例生 ...

  9. GNU C编译器的gnu11和c11

    国际标准组织发布c11后,gnu为自己的编译器发布两种标准gnu11和c11 gnu11:带gnu c扩展的c11标准,如果你的代码包含了typeof,__attribute__等等gnu的扩展,就必 ...

  10. firefox因 HTTP 严格传输安全(HSTS)机制无法打开网页

    1.打开about:config 2.查找: security.enterprise_roots.enabled ,默认为false,改为true就可以了 3.吐槽,firefox太极端了,这是作死.