Leetcode #7 revert integer problem

Source: Internet
Author: User

Problem Description:

Reverse digits of an integer.

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

Enter an integer number and then output its reversal

It should be noted that the reversal of the int variable may be more than the limit of int, such as 12345678999 reversal, it would be overflow,leetcode to return 0 if this condition occurs.

The idea is to get the value of each digit of the input number from the last loop and put it in a new number. The type of the returned number is set to long, and if the bounds of int are exceeded, return 0.

classSolution { Public:    LongReverseintx) {//Start Typing your/C + + solution below//Do not write int main () function        intSign = x >0?1: -1; X=ABS (x); LongRET =0;  while(x) {intdigit=x%Ten; RET=ret*Ten+Digit; X/=Ten; }        if(ABS (ret) >Int_max)return 0; returnRET *Sign ; }};

Leetcode #7 revert integer problem

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.