[Leetcode] Reverse Integer

Source: Internet
Author: User

Reverse digits of an integer.

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

Click to show spoilers.

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.

Update (2014-11-10):

Test cases had been added to test the overflow behavior.

The question was updated on November 10, adding a test case against the transfer overflow. So we have to find out the number and then click it and return 0.

First of all, there is no update before the AC code, for everyone to familiarize themselves with the idea.

Class Solution {public:    int reverse (int x) {        int flag = (x > 0)? 1:-1;        int y =abs (x);        int result = 0;        while (y) {            result = result*10 + y%10;            y = Y/10;        }        return flag*result;}    ;
The idea is simple, is to take the last one added to the result, and then multiplied by 10, so reciprocating, take the highest bit of the original number, added to the result, the highest bit of result is exactly the lowest bit of the original number.

But now the problem is, if the reversal number of the original number overflows, then it is wrong to write.

Now think about it, as long as the number of reversal is larger than Int_max, it will overflow, this number in memory has changed, so there is no way to compare it and int_max size. So here's another way of thinking, comparing the size of the previous step and the INT_MAX/10, if a step result is larger than INT_MAX/10, and then y > 0, the result overflows.

This code is AC, but think carefully about bringing a few spies in. If a number, flip the penultimate result = = INT_MAX/10, and the remaining highest bit y>int_max%10, will overflow, so you may want to exclude this part.

This method is actually in the original method to make a patch, really can't think of a better way.

There is another is int_min need to exclude, this goods take absolute value than Int_max big 1, direct overflow.

Class Solution {public:    int reverse (int x) {        if (x = = Int_min) return false;        int flag = (x > 0)? 1:-1;        int y =abs (x);        int result = 0;        while (y) {            result = result*10 + y%10;            y = Y/10;            if (result> (INT_MAX/10) &&y > 0 | | result== (INT_MAX/10) &&y>int_max%10) return false;        }        return flag*result;}    ;

I hope we can give a good idea, if you think of a better way to remember to tell me.


[Leetcode] Reverse Integer

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.