Leetcode 7 Reverse Integer (C,c++,java,python)

Source: Internet
Author: User

Problem:

Reverse digits of an integer.

Example1: x = 123, return 321
Example2: x = -123, return-321

Has a thought about this ?

Here is some good questions to ask before coding. Bonus points for if you have already thought through this!

If the last digit are 0, what should the output being? ie, cases such as 10, 100.

Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer and then the reverse of 1000000003 overflows. How should handle such cases?

For the purpose of this problem, assume a 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 don't need to check if ret = = 214748364, why?

Average rating:3.5 (Votes)

The main idea: to give an integer, required to reverse the integer to get another integer, if the inverse of the integer out of bounds, return 0, problem-solving ideas: directly with res=res*10+x/10;x/=10; Cross-border judgment method to convert to double type and then determine whether the result exceeds Int_max (2147483647)
Java source code (spents 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 Language Source code (spents 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 + + source code (spents 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 source code (spents 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< c9/>x/=10        return Res*flag


Leetcode 7 Reverse Integer (C,c++,java,python)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.