7 Reverse Integer (digital inversion Easy)

Source: Internet
Author: User

Title meaning: int number reversal

Considerations: Cross-border issues

1 classSolution {2  Public:3     intReverseintx) {4         Longans=0;5          while(x) {6ans=ans*Ten+x%Ten;7x=x/Ten;8         }9         return(Ans > Int_max | | ans < int_min)?0: ans;Ten     } One};

Long in Ps:leetcode is longer than int, but in Visual C + +, long and int take the same range of values

So here's the code that I think is better.

1 classSolution {2  Public:3     intReverseintx) {4         intans=0;5          while(x>=Ten|| x<=-Ten){6ans=ans*Ten+x%Ten;7x=x/Ten;8         }9         if(ans>int_max/Ten|| (ans==int_max/Ten&&x>int_max%Ten))Ten             return 0; One         if(ans<int_min/Ten|| (ans==int_min/Ten&&x<int_min%Ten)) A             return 0; -         returnans*Ten+x%Ten; -     } the};

   

7 Reverse Integer (digital inversion Easy)

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.