7.力扣题目_整数反转

整数反转

给你一个 32 位的有符号整数 x ,返回将 x 中的数字部分反转后的结果。

如果反转后整数超过 32 位的有符号整数的范围 [−231, 231 − 1] ,就返回 0。

假设环境不允许存储 64 位整数(有符号或无符号)。

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);
      
   }
}

猜你喜欢

转载自blog.csdn.net/GoodCooking/article/details/130662651
今日推荐