Leetcode-Day 11

Source: Internet
Author: User

Question 1Reverse Integer

Reverse digits of an integer.

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

We need to consider a lot of cases of input. One particular example is what to deal with many zeros in the end of the.

The most direct-to-use string or char array to store the int number and then convert it. But I prefer to use the pure math-to-do it as cleaner and easier.

1  Public classSolution {2      Public intReverseintx) {3         if(x>=0 && x < 10)4             returnx;5         6         BooleanFlag =true;7         8         if(X < 0){9x = 0-x;TenFlag =false; One         } A          -         Doubleresult = 0; -          the          while(X > 0){ -             intMoD = x% 10; -result = result * 10 +MoD; -x = X/10; +         } -          +         if(flag) A             returnResult > Integer.max_value? 0: (int) result; at         Else -             return0-result < Integer.min_value? 0: (int) (0-result); -          -     } -}

Leetcode-Day 11

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.