integer inversion
Given a 32-bit signed integer x, return the result of inverting the numeric portion of x.
Returns 0 if the inverted integer exceeds the range [−231, 231 − 1] of a 32-bit signed integer.
Assume the environment does not allow storing 64-bit integers (signed or unsigned).
public class Solution {
public int Reverse(int x) {
bool addF = false;
if (x >= 0)
{
addF = false;
}
else
{
addF = true;
}
if (x == -2147483648)
{
return 0;
}
string numx = Math.Abs(x).ToString();
numx.Replace("-", "");
List<char> numxList = numx.Reverse().ToList();
string numxR = "";
for (int i = 0; i < numx.Length; i++)
{
numxR = numxR + numxList[i];
}
if (Int64.Parse(numxR) < -(2147483648))
{
Console.WriteLine("大于最小的");
return 0;
}
if (Int64.Parse(numxR) > (2147483648 - 1))
{
Console.WriteLine("小于最大的");
return 0;
}
if (addF)
{
return -1 * int.Parse(numxR);
}
return int.Parse(numxR);
}
}