• 整数反转


    给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转。

    注意:

    假设我们的环境只能存储得下 32 位的有符号整数,则其数值范围为 [−231,  231 − 1]。请根据这个假设,如果反转后整数溢出那么就返回 0。

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/reverse-integer

     1 public int Reverse(int x)
     2         {
     3             int num = 0;
     4             bool isFlag = true;
     5 
     6             if (x < 0)
     7             {
     8                 x = x * -1;
     9                 isFlag = false;
    10             }
    11             while (x > 0)
    12             {
    13                 int a = x % 10;
    14                 if ((a == 0 && num != 0) || a != 0)
    15                 {
    16                     int b = num * 10;
    17                     if (b / 10 != num)
    18                     {
    19                         num = 0;
    20                         break;
    21                     }
    22                     num = num * 10 + a;
    23 
    24                 }
    25                 x = x / 10;
    26             }
    27             if (!isFlag)
    28             {
    29                 num = num * (-1);
    30             }
    31             return num;
    32         }
  • 相关阅读:
    HashSet源码分析
    Mysql的体系结构和存储引擎
    触发器
    存储过程和函数
    索引
    SpringBoot 中的日志使用
    log4j2
    Logback
    slf4j
    日志门面
  • 原文地址:https://www.cnblogs.com/HYJ0201/p/11645092.html
Copyright © 2020-2023  润新知