题目:

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. Jquery重新学习之二[属性attr(),removeAttr(),prop(),removeProp()]

    1:属性.attr(name|pro|key,val|key,fn)与removeAttr(name) 1.1 .attr(name) 参数name为属性名称 <a id="my_hr ...

  2. Generator [ˈdʒenəreɪtə(r)] 函数结构

    Generator函数是ES6新增的一种异步编程方案. 说明:Generator函数指的是一种新的语法结构,是一个遍历器对象生成器,它内部可以封装多个状态,非常适合用于异步操作. Generator函 ...

  3. 揭秘ThreadLocal(转)

    转载自:掘金大闲人柴毛毛博客. ThreadLocal是开发中最常用的技术之一,也是面试重要的考点.本文将由浅入深,介绍ThreadLocal的使用方式.实现原理.内存泄漏问题以及使用场景. Thre ...

  4. checkbox显示选中内容个数

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  5. 【LeetCode】99. Recover Binary Search Tree

    Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...

  6. zookeeper集群的搭建

    虚拟机为VirtualBox 4.3.12版本:系统为CentOS 6.6版本:zookeeper 3.4.7:java版本1.7.0_67. 虚拟机设置为桥接网卡,这样就可以通过主机网卡上网了.Vi ...

  7. Android屏幕计量单位详解

    1.px (pixels)(像素):是屏幕的物理像素点,与密度相关,密度大了,单位面积上的px会比较多.通常不推荐使用这个. 2.dip或dp(与密度无关的像素):一个基于density的抽象单位,这 ...

  8. some issues that you should be take care of when use the plupload module

    1. the config arguments 'browse_button' should not be a single element like button etc. because in i ...

  9. xdebug安装教程

    自动分析应该下载的文件: http://xdebug.org/wizard.php

  10. sed n/N使用说明

    sed的语法格式: sed [option] {sed-command} {input-file} sed在正常情况下,将处理的行读入模式空间(pattern space),脚本中的“sed-comm ...