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)
时间: 2024-11-06 07:17:18

leetcode_num4_Reverse Integer的相关文章

Integer和Long部分源码分析

Integer和Long的java中使用特别广泛,本人主要一下Integer.toString(int i)和Long.toString(long i)方法,其他方法都比较容易理解. Integer.toString(int i)和Long.toString(long i),以Integer.toString(int i)为例,先看源码: 1 /** 2 * Returns a {@code String} object representing the 3 * specified intege

[LeetCode] 12. Integer to Roman ☆☆

Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. 解释: 罗马数字采用七个罗马字母作数字.即Ⅰ(1).X(10).C(100).M(1000).V(5).L(50).D(500).记数的方法: 相同的数字连写,所表示的数等于这些数字相加得到的数,如 Ⅲ=3: 小的数字在大的数字的右边,所表示的数等于这些数字相加得到的数,如 Ⅷ=8.

Java进阶(三十四)Integer与int的种种比较你知道多少?

Java进阶(三十四)Integer与int的种种比较你知道多少? 前言 如果面试官问Integer与int的区别:估计大多数人只会说到两点:Ingeter是int的包装类,注意是一个类:int的初值为0,Ingeter的初值为null.但是如果面试官再问一下Integer i = 1;int ii = 1; i==ii为true还是为false?估计就有一部分人答不出来了,如果再问一下其他的,估计更多的人会头脑一片混乱.所以我对它们进行了总结,希望对大家有帮助. 首先看代码: package

Python integer objects implementation

http://www.laurentluce.com/posts/python-integer-objects-implementation/ Python integer objects implementation May 15, 2011 This article describes how integer objects are managed by Python internally. An integer object in Python is represented interna

Reverse Integer - 反转一个int,溢出时返回0

Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 若反转的数溢出,直接返回0 可以用计算结果来判断溢出,也可以用因数来判断 Java代码实现: 1 public class ReverseInteger { 2 public static int reverseInt(int x){ 3 if (x == 0) { 4 return

Reverse Integer

Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have already thought throug

LeetCode:Integer to Roman

1.题目名称 Integer to Roman (阿拉伯数字到罗马数字的转换) 2.题目地址 https://leetcode.com/problems/integer-to-roman 3.题目内容 英文:Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. 中文:给出一个整数,将它转换成罗马数字.输入在1-3999之间. 4.题目分

LeetCode:String to Integer (atoi)

1.题目名称 String to Integer (atoi) (字符串到数字的转换) 2.题目地址 https://leetcode.com/problems/string-to-integer-atoi/ 3.题目内容 英文:Implement atoi to convert a string to an integer. 中文:实现atoi函数,将输入的字符串(String类型)转换为整型数据(Integer类型) 提示:实现的atoi函数需要满足以下特征 忽略字符串第一个非空格字符前的所

leetcode Roman to integer

题目: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 要把罗马数字转换为整数, 罗马数字自行百度 code: class Solution { public: int romanToInt(string s) { map<char,int> Roman; Roman['I'] = 1; Roman['V'] = 5; Roma