[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

题意

反转一个数。

分析

注意读入和返回的数都是 int 型的,这时就要考虑反转后这个数会不会超 int,超的话就返回 0 。这时处理数时最好用比 int 大的类型,不然恐怕会超范围。

当然也可以用 int :if (result > (INT_MAX/10))

还有一点就是还要考虑前导零。

代码

C++:

class Solution {
public:
    int reverse(int x) {
		long long tmp = abs((long long)x);
		long long ret = 0;
		while (tmp) {
			ret = ret * 10 + tmp % 10;
			if (ret > INT_MAX)
				return 0;
			tmp /= 10;
		}
		if (x > 0)
			return (int)ret;
		else
			return (int)-ret;
    }
};

Java:

public class Solution {

    public int reverse(int x) {
        int ret = 0;
        while (Math.abs(x) != 0) {
            if (Math.abs(ret) > Integer.MAX_VALUE)
                return 0;
            ret = ret * 10 + x % 10;
            x /= 10;
        }
        return ret;
    }
}

Python:

class Solution:
    # @return an integer
    def reverse(self, x):
        revx = int(str(abs(x))[::-1])
        if revx > math.pow(2, 31):
            return 0
        else:
            return revx * cmp(x, 0)
时间: 2024-11-18 22:11:41

[LeetCode] 007. Reverse Integer (Easy) (C++/Java/Python)的相关文章

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

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] 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] 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 题意: 判断一个括号字符串是否是有效的. 分析:

[LeetCode] 006. ZigZag Conversion (Easy) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 006.ZigZag_Conversion (Easy) 链接: 题目:https://oj.leetcode.com/problems/zigzag-conversion/ 代码(github):https://github.com/illuz/leetcode 题意: 把一个字符串按横写的折线排列. 分析: 直

[LeetCode] 009. Palindrome Number (Easy) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 009.Palindrome_Number (Easy) 链接: 题目:https://oj.leetcode.com/problems/palindrome-number/ 代码(github):https://github.com/illuz/leetcode 题意: 判断一个数是否是回文数. 分析: 按自己想

Java for LeetCode 007 Reverse Integer

Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 解题思路:将数字翻转并不难,可以转成String类型翻转,也可以逐位翻转,本题涉及到的主要是边界和溢出问题,使用Long或者BigInteger即可解决. 题目不难: JAVA实现如下: public class Solution { static public int reverse(int x) { if(x=

LeetCode 007 Reverse Integer - Java

Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Note:The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows. 定位:简单题 将输入的数反转输出,注意的是负数符号保持在最前,反转后的