Lintcode Easy title: Reverse integer Upside down

Source: Internet
Author: User
Tags lintcode

Topic:

Reverse integer

Reverses the number in an integer and returns 0 (marked as a 32-bit integer) when the inverted integer overflows.

Sample Example

Given x = 123 , returns321

Given x = -123 , returns-321

Solving:

Direct inversion, cross-border processing of scrambled eggs.

Java Program:

 Public classSolution {/**     * @paramn the integer to be reversed *@returnThe reversed integer*/     Public intReverseinteger (intN) {//Write Your code here        intMAX =Integer.max_value; if(n>=0){            intres = 0; intnum =N;  while(n!=0){                if(RES>MAX/10)return0; Res=res *10 + n%10; N= N/10; }            returnRes; }Else{            intres = Reverseinteger (-N); return-Res; }            }}
View Code

Total time: 16030 ms

Python program:

Not yet, Pending,python. No need to deal with cross-border issues

classSolution:#@param {int} n the integer to is reversed    #@return {int} The reversed integer    defReverseinteger (self, n):#Write Your code hereFlag =Falseifn<0:n= -N Flag=True Res=0 whilen!=0:res= Res * + n%10; N= N/10ifflag:return-ResElse:            returnRes
View Code

Lintcode Easy title: Reverse integer Upside down

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.