public class Solution {
public int Reverse(int x) {
int fuhao = ;
if (x < )
{
fuhao = -;
} try
{
x = Math.Abs(x);
}
catch (Exception e)
{
return ;
} int i = ; var list = new List<long>(); do
{
long num = x % Convert.ToInt64(Math.Pow(, i + )) / Convert.ToInt64(Math.Pow(, i));
list.Add(num);
i++;
}
while (x / Convert.ToInt64(Math.Pow(, i)) != ); var length = list.Count; long result = ; for (int j = ; j < length; j++)
{
try
{
result += list[j] * Convert.ToInt64(Math.Pow(, length - j - ));
}
catch (Exception e)
{
result = ;
break;
}
} result *= fuhao; int res = ; int.TryParse(result.ToString(), out res); //Console.WriteLine(res);
return res;
}
}

https://leetcode.com/problems/reverse-integer/#/description

补充一个python的实现:

 class Solution:
def reverse(self, x: int) -> int:
if x == :
return
operation =
if x < :
operation = -
sx = str(x)
if sx[] == '-':
sx = sx[:]
n = len(sx)
result = []
for i in range(n-,-,-):
result.append(sx[i])
r = int(''.join(result))
r *= operation
if r >= ** or r < (-) ** :
r =
return r
05-01 03:57