LeetCode7 Reverse Integer

Source: Internet
Author: User

Test instructions

Reverse digits of an integer.

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

Analysis:

The idea is very simple, pay attention to special cases such as 10,100 and int overflow situation can be;

Start with an array to save you first, and then multiply the corresponding coefficients to make the result, but the code is lengthy and cost space;

A while loop can be used directly;

Code Listing 1:

1 classSolution {2  Public:3     intReverseintx) {4         Long Longresult =0;5          while(X! =0) {6             inttemp = x%Ten;7X/=Ten;8result = result *Ten+temp;9             if(Result >0x7FFFFFFF|| Result <-0x7FFFFFFF) {Ten                 return 0; One             } A         } -         returnresult; -     } the};

Viewing the discussion area, it is found that there is a kind of realization method without 0x7fffffff judgment, as reference;

Code Listing 2:

1 classSolution {2  Public:3     intReverseintx) {4         intresult =0;5          while(X! =0) {6             inttemp = x%Ten;7X/=Ten;8             intNewresult = result *Ten+temp;9             if((newresult-temp)/Ten!=result) {Ten                 return 0; One             } Aresult =Newresult; -         } -         returnresult; the     } -};

LeetCode7 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.