LeetCode 7 Reverse Integer(C,C++,Java,Python)

Problem:

Reverse digits of an integer.

Example1: x = 123, return 321

Example2: x = -123, return -321

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?

For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

Solution:

To check for overflow/underflow, we could check if ret > 214748364 or ret < –214748364 before multiplying by 10. On the other hand, we do not need to check if ret == 214748364, why?

Cancel rating
1
2
3
4
5

Average Rating: 3.5 (360 votes)

题目大意:

给一个整数,要求将整数逆序得到另外一个整数,如果逆序后的整数越界,返回0,

解题思路:

直接用res=res*10+x/10;x/=10;来解决,注意判断越界,越界判断方法为转化为double类型然后判断结果是否超出INT_MAX(2147483647)

Java源代码(用时228ms):

public class Solution {
    public int reverse(int x) {
        int flag=x>0?1:-1,res=0;
        x=x>0?x:-x;
        while(x>0){
            if(res*10.0 + x%10 > 2147483647)return 0;
            res = res*10+x%10;
            x/=10;
        }
        return res*flag;
    }
}

C语言源代码(用时10ms):

int reverse(int x) {
    int flag=x>0?1:-1,res=0;
    x=x>0?x:-x;
    while(x>0){
        if((2147483647.0-x%10)/10<res)return 0;
        res=res*10+x%10;
        x=x/10;
    }
    return res*flag;
}

C++源代码(用时13ms):

class Solution {
public:
    int reverse(int x) {
        int flag=x>0?1:-1,res=0;
        x=x>0?x:-x;
        while(x>0){
            if(res*10.0+x%10 > 2147483647)return 0;
            res = res*10+x%10;
            x/=10;
        }
        return res*flag;
    }
};

Python源代码(用时72ms):

class Solution:
    # @param {integer} x
    # @return {integer}
    def reverse(self, x):
        flag=1 if x>0 else -1
        x=x if x>0 else -x
        res=0
        while x>0:
            if res*10.0 + x%10 > 2147483647:return 0
            res = res*10+x%10
            x/=10
        return res*flag

时间: 2024-09-28 22:42:13

LeetCode 7 Reverse Integer(C,C++,Java,Python)的相关文章

[LeetCode] 007. Reverse Integer (Easy) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 007.Reverse_Integer (Easy) 链接: 题目:https://oj.leetcode.com/problems/Reverse-Integer/ 代码(github):https://github.com/illuz/leetcode 题意: 反转一个数. 分析: 注意读入和返回的数都是 in

LeetCode:Reverse Integer - 翻转数字

1.题目名称 Reverse Integer(翻转数字) 2.题目地址 https://leetcode.com/problems/reverse-integer/ 3.题目内容 英文:Reverse digits of an integer. 中文:翻转一个正整数的各位,形成一个新数字 例如:x = 123, return 321:x = -123, return -321 4.一个有瑕疵的方法(不能AC) 一个比较好想到的方法,是先将输入的数字转换为字符串,再将字符串翻转后转换为数字.这个方

LeetCode 007 Reverse Integer

[题目] Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 [题意] 反转int型整数,输出的也是int型的整数 [思路] 如要考虑两种特殊情况: 1. 类似100这样的整数翻转之后为1 2. 翻转之后的值溢出该如何处理, 本题的测试用例中似乎没有给出溢出的情况 在实际面试时需要跟面试官明确这种情况的处理方法. 基于这点事实,本题规定如果超出正边界返回INT_MA

Leetcode 数 Reverse Integer

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Reverse Integer Total Accepted: 17472 Total Submissions: 43938 Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to show spoilers. Have you thought

【LeetCode】Reverse Integer (2 solutions)

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 alread

[LeetCode] 013. Roman to Integer (Easy) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 013.Roman_to_Integer (Easy) 链接: 题目:https://oj.leetcode.com/problems/roman-to-integer/ 代码(github):https://github.com/illuz/leetcode 题意: 把罗马数转为十进制. 分析: 跟 012. I

[LeetCode] 008. String to Integer (Easy) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 008.String_to_Integer (Easy) 链接: 题目:https://oj.leetcode.com/problems/string-to-integer-atoi/ 代码(github):https://github.com/illuz/leetcode 题意: 将一个字符串转化为 int 型.

LeetCode 7 Reverse Integer(反转数字)

题目来源:https://leetcode.com/problems/reverse-integer/ Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 解题思路: 其实这道题看起来非常简单,要实现也是几行代码的事.但是有个小问题容易被忽略,就是边界问题.什么意思呢?如果我们输入的整数超出了int的表达范围,这个问题要怎么解决呢? 用比int更大的数据类型存储我们转

[LeetCode] 020. Valid Parentheses (Easy) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 020.Valid_Parentheses (Easy) 链接: 题目:https://oj.leetcode.com/problems/valid-parentheses/ 代码(github):https://github.com/illuz/leetcode 题意: 判断一个括号字符串是否是有效的. 分析: