Reverse Integer Leetcode Java

Source: Internet
Author: User

Problem Description:

Reverse digits of an integer.

EXAMPLE1:X = 123, return 321
example2:x = -123, return-321

Analysis: the output of an integer in reverse order, you need to determine whether out of bounds. Integer.max_value,integer.min_value, because a positive and negative cross-boundary judgment is to be made, a negative number is converted to an integer .

Code

 Public intReverseintx) {intRET = 0; intDigit = 0; BooleanNeg_flag =false; if(X < 0) {Neg_flag=true; X=-1 * x;//Covert to ABS (x), and record the symbol of negative or positive.        }                     while(x! = 0) {digit= x% 10;//get the last digit of x, get the number at the end of X//if ret overflows?            if(ret! = 0) {//must follow this pattern to check                if((integer.max_value-digit)/RET < 10 )                     return0; if(Neg_flag = =true) {                    if( -10 < (integer.min_value + digit)/ret)//-(ret * + digit) < Integer.min_value//If we convert the number to ABS, we need to compare it in negative form with Integer.min_value                   return0; }} RET= RET * 10 +Digit; X= X/10;//chop off the last digit of x, cut the end number of x        }                if(Neg_flag = =true) ret=-1 *ret; returnret; }

Reverse Integer Leetcode Java

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.