Leetcode 007 Reverse Integer-java

Source: Internet
Author: User

Reverse digits of an integer.

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

Note:
The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows.

Positioning: Simple Questions

Reverses the output of the input, noting that the negative sign remains at the top, and the inverted value exceeds the Int_32bit range output 0.

Simple to pass as the number of the separation symbol after the reversal with StringBuffer, and then converted to int, at this time try to grab numberformatexception error, if there is a direct 0.

Java implementations:

1  Public classSolution {2      Public intReverseintx) {3         Booleanisnev=false;4         if(x<0){5x=-x;6isnev=true;7         }8StringBuffer stringbuffer=NewStringBuffer (string.valueof (x));9Stringbuffer=stringbuffer.reverse ();Ten         inty; One         Try{ Ay=Integer.parseint (stringbuffer.tostring ()); -}Catch(NumberFormatException e) { -Y=0; the         } -         if(Isnev) { -y=-y; -         } +         returny; -     } +}

Leetcode 007 Reverse Integer-java

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.