Given a 32-bit signed integer, reverse digits of an integer
/*
* Given a 32-bit signed integer, reverse digits of an integer
*
* integer range : [-2^31, 2^31-1]
*
* Input : 123
* Output : 321
*
* Input : -123
* Output : -321
*
* Input : 120
* Output : 21
*
* Input : 0
* Output : 0
*/
public int LeetCode_7(int x)
{
int ans = 0;
string s1 = "";
while(x != 0)
{
int t = x % 10;
int newAns = ans * 10 + t;
if ((newAns - t) / 10 != ans)
return 0;
ans = newAns;
x = x / 10;
}
/*
if (x != 0)
{
s1 = x.ToString();
while(true)
{
if (s1.Substring(s1.Length-1, 1) == "0")
s1 = s1.Remove(s1.Length-1, 1);
else
break;
}
s1 = string.Join("",s1.Reverse());
if (x < 0)
s1 = "-" + s1.Remove(s1.Length - 1, 1);
try
{
ans = (Int32)Convert.ToInt32(s1);
}
catch(Exception)
{
ans = 0;
}
}
else
{
ans = 0;
}
*/
return ans;
}