【问题标题】:Reverse Integer (Leetcode)反转整数(Leetcode)
【发布时间】:2022-01-15 14:04:58
【问题描述】:

我正在尝试解决 leetcode https://leetcode.com/problems/reverse-integer/ 上的 7.Reverse Integer。

给定一个有符号的 32 位整数 x,返回 x 的数字反转。如果反转 x 导致值超出有符号 32 位整数范围 [-2^31, 2^31 - 1],则返回 0。

示例 1:

Input: x = 123  
Output: 321

我对上述问题的解决方案是

class Solution {  
    public int reverse(int x) {  
        int num=0;  
        if(x>Integer.MAX_VALUE||x<Integer.MIN_VALUE) return 0;  
        while(x!=0){  
            int a=x%10;  
            num=num*10+a;  
            x=x/10;  
            
            
        }  
        return num;  
    }  
}  

我弄错了 4 个测试用例。其中之一是:

示例

Input: 1534236469  
Output : 1056389759  
Expected: 0  

【问题讨论】:

  • 您需要考虑整数溢出以及如何避免它。 0 的预期答案告诉您简单的反转超出范围。
  • "如果反转 x 导致值超出有符号的 32 位整数范围 [-2^31, 2^31 - 1],则返回 0。"

标签: java algorithm integer reverse


【解决方案1】:

您的问题是溢出在num 变量中,而您没有对此进行检查。通过在执行num = num*10+a之前添加检查以确保计算不会溢出,您可以在必要时返回0

另外,您没有正确处理负数。预先检查是否为负数可以让您使用正数,然后将结果取反。

class Solution {  
    public int reverse(int x) {  
        int num=0;  
        Boolean negative = false;
        
        if (x < 0) {
            x = -x;
            negative = true;
        }
        while(x!=0){  
            int a=x%10; 
            // Check if the next operation is going to cause an overflow
            // and return 0 if it does
            if (num > (Integer.MAX_VALUE-a)/10) return 0;
            num=num*10+a;  
            x=x/10;  
        }  
        return negative ? -num : num;  
    }  
} 

【讨论】:

    【解决方案2】:

    您选择的方法并不遥远。

    1. 您当前检查输入x 是否在无符号整数范围内。但他们要求检查 x-reversed。
    2. 您将答案汇总为一个整数,因此您可能会溢出而被忽视。

    如果您将结果 num 聚合到 long 类型的变量中,如果在反转后答案超出 unsigned int 的范围,则拒绝/归零答案,这两个问题都可以解决。

    您可以使用 Math.addExact(a, b)、Math.multiplyExact(a,b) 和 try-catch 在溢出时立即退出。

    【讨论】:

      【解决方案3】:

      输入:123 输出:321 输入:-123 输出:-321 输入:120 输出:2

      类解决方案{ 上市: int 反向(int x){

          int rev = 0;
      
          constexpr int top_limit = INT_MAX/10;
      
          constexpr int bottom_limit = INT_MIN/10;
      
          while (x) {
      
              if (rev > top_limit || rev < bottom_limit)
      
                  return 0;
      
              rev = rev * 10 + x % 10;
      
              x /= 10;
          }
          return rev;
      }
      

      };

      【讨论】:

      • 一个好的答案将始终包括解释为什么这会解决问题,以便 OP 和任何未来的读者可以从中学习。
      • 看起来不像 Java...
      【解决方案4】:

      您没有处理循环中可能发生的理论上的有符号 32 位整数溢出,这意味着您有时会返回该范围之外的数字。此外,对于负值,逻辑将无法按预期工作。

      为了真正精确地限制带符号的 32 位,当输入为 -231 时需要特别注意,因为它的绝对值不代表有效的带符号 32-位整数。

      class Solution {
          public int reverse(int x) {
              if (x < 0) return x == -2147483648 ? 0 : -reverse(-x);
              int res = 0;
              while (x > 0 && res < 214748364) {
                  res = res * 10 + x % 10;
                  x /= 10;
              }
              return x == 0 ? res
                   : res > 214748364 || x > 7 ? 0
                   : res * 10 + x;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-01-14
        • 2022-07-08
        • 2022-01-13
        • 1970-01-01
        • 2022-07-30
        • 2021-10-01
        • 2022-01-18
        • 2013-04-15
        相关资源
        最近更新 更多