7. Reverse Integer Java Solutions

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.

Subscribe to see which companies asked this question

According to test instructions, it is necessary to consider the case at the end of 0, the overflow condition, the case of negative numbers, etc.
1  Public classSolution {2      Public intReverseintx) {3         BooleanIsnegative =false;4         if(x < 0) isnegative =true;5         LongAns = 0;6x =Math.Abs (x);7          while(X > 0){8Ans = (ans*10) + (x%10);9X/= 10;Ten         } One         if(Ans > Integer.max_value | | ans < integer.min_value)return0; A         if(isnegative) ans =-ans; -         return(int) ans; -     } the}

7. Reverse Integer Java Solutions

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.