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