LinkSearchMenuExpandDocument

7. Reverse Integer

Solution Code

C#

publicclassSolution{publicintReverse(intx){boolisNegative=false;if(x<0){isNegative=true;x*=-1;}longrev=0;for(;x>0;rev=rev*10+(x%10),x/=10);if(rev>((1L<<31)-1))return0;if(isNegative)rev*=-1;return(int)rev;}}

C++

classSolution{public:intreverse(intx){signedlonglongrev=0,num=x;boolis_negative=false;if(num<0){is_negative=true;num*=-1;}for(;num>0;rev=rev*10+(num%10),num/=10);if(rev>((1LL<<31)-1))return0;if(is_negative)rev*=-1LL;return(int)rev;}};

Java

classSolution{publicintreverse(intx){booleanisNegative=false;if(x<0){isNegative=true;x*=-1;}longrev=0;for(;x>0;rev=rev*10+(x%10),x/=10);if(rev>((1L<<31)-1))return0;if(isNegative)rev*=-1;return(int)rev;}}

© 2023. All rights reserved.

close