Skip to content

Instantly share code, notes, and snippets.

@AbhiAgarwal192
Created August 20, 2020 14:11
Show Gist options
  • Save AbhiAgarwal192/cf28a2b3a06717df60d50613076d4d42 to your computer and use it in GitHub Desktop.
Save AbhiAgarwal192/cf28a2b3a06717df60d50613076d4d42 to your computer and use it in GitHub Desktop.
Given a 32-bit signed integer, reverse digits of an integer.
public class Solution {
public int Reverse(int x) {
if(x<=Int32.MinValue){
return 0;
}
int temp = x;
long rev = 0;
temp = Math.Abs(temp);
while(temp>0){
int rem = temp%10;
temp = temp/10;
rev = rev*10 + rem;
}
if(rev>Int32.MaxValue){
return 0;
}
if(x<0){
rev = 0-rev;
}
if(rev<Int32.MinValue){
return 0;
}
return Convert.ToInt32(rev);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment