7.Reverse Integer (INT; Overflow)

Source: Internet
Author: User

Reverse digits of an integer.

Example1: x = 123, return 321
Example2: x = -123, return-321

Idea: be aware of overflow. Use the following procedure to overflow.

class Solution {public:    int reverse (int  x) {        int 0 ;          while (x) {            ten + xpercent;             Ten ;        }         return ret;    }};

An improved approach:

classSolution { Public:    intReverseintx) {if(x = = int_min)return 0; intRET =0; intDigit; BOOLpos = x>=0?true:false; X= ABS (x);//now the negative modulo compiler is not uniform, so it becomes a positive action         while(x) {digit= x percentTen; if(Ret > (int_max-digit)/Ten)//10*ret+digit > Int_max                return 0; RET= RET *Ten+Digit; X/=Ten; }        if(POS)returnret; Else return(-ret); }};

7.Reverse Integer (INT; Overflow)

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.