返回介绍

solution / 0000-0099 / 0029.Divide Two Integers / README_EN

发布于 2024-06-17 01:04:40 字数 7479 浏览 0 评论 0 收藏 0

29. Divide Two Integers

中文文档

Description

Given two integers dividend and divisor, divide two integers without using multiplication, division, and mod operator.

The integer division should truncate toward zero, which means losing its fractional part. For example, 8.345 would be truncated to 8, and -2.7335 would be truncated to -2.

Return _the quotient after dividing _dividend_ by _divisor.

Note: Assume we are dealing with an environment that could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For this problem, if the quotient is strictly greater than 231 - 1, then return 231 - 1, and if the quotient is strictly less than -231, then return -231.

 

Example 1:

Input: dividend = 10, divisor = 3
Output: 3
Explanation: 10/3 = 3.33333.. which is truncated to 3.

Example 2:

Input: dividend = 7, divisor = -3
Output: -2
Explanation: 7/-3 = -2.33333.. which is truncated to -2.

 

Constraints:

  • -231 <= dividend, divisor <= 231 - 1
  • divisor != 0

Solutions

Solution 1: Simulation + Fast Power

Division is essentially subtraction. The problem requires us to calculate the integer result after dividing two numbers, which is actually calculating how many divisors and a number less than the divisor constitute the dividend. However, only one subtraction can be done in one loop, which is too inefficient and will lead to timeout. This can be optimized by using the idea of fast power.

It should be noted that since the problem explicitly requires that only 32-bit signed integers can be used at most, the divisor and dividend need to be converted to negative numbers for calculation. Because converting to positive numbers may cause overflow, such as when the dividend is INT32_MIN, it will be greater than INT32_MAX when converted to a positive number.

Assuming the dividend is $a$ and the divisor is $b$, the time complexity is $O(\log a \times \log b)$, and the space complexity is $O(1)$.

class Solution:
  def divide(self, a: int, b: int) -> int:
    if b == 1:
      return a
    if a == -(2**31) and b == -1:
      return 2**31 - 1
    sign = (a > 0 and b > 0) or (a < 0 and b < 0)
    a = -a if a > 0 else a
    b = -b if b > 0 else b
    ans = 0
    while a <= b:
      x = b
      cnt = 1
      while x >= (-(2**30)) and a <= (x << 1):
        x <<= 1
        cnt <<= 1
      a -= x
      ans += cnt
    return ans if sign else -ans
class Solution {
  public int divide(int a, int b) {
    if (b == 1) {
      return a;
    }
    if (a == Integer.MIN_VALUE && b == -1) {
      return Integer.MAX_VALUE;
    }
    boolean sign = (a > 0 && b > 0) || (a < 0 && b < 0);
    a = a > 0 ? -a : a;
    b = b > 0 ? -b : b;
    int ans = 0;
    while (a <= b) {
      int x = b;
      int cnt = 1;
      while (x >= (Integer.MIN_VALUE >> 1) && a <= (x << 1)) {
        x <<= 1;
        cnt <<= 1;
      }
      ans += cnt;
      a -= x;
    }
    return sign ? ans : -ans;
  }
}
class Solution {
public:
  int divide(int a, int b) {
    if (b == 1) {
      return a;
    }
    if (a == INT_MIN && b == -1) {
      return INT_MAX;
    }
    bool sign = (a > 0 && b > 0) || (a < 0 && b < 0);
    a = a > 0 ? -a : a;
    b = b > 0 ? -b : b;
    int ans = 0;
    while (a <= b) {
      int x = b;
      int cnt = 1;
      while (x >= (INT_MIN >> 1) && a <= (x << 1)) {
        x <<= 1;
        cnt <<= 1;
      }
      ans += cnt;
      a -= x;
    }
    return sign ? ans : -ans;
  }
};
func divide(a int, b int) int {
  if b == 1 {
    return a
  }
  if a == math.MinInt32 && b == -1 {
    return math.MaxInt32
  }

  sign := (a > 0 && b > 0) || (a < 0 && b < 0)
  if a > 0 {
    a = -a
  }
  if b > 0 {
    b = -b
  }
  ans := 0

  for a <= b {
    x := b
    cnt := 1
    for x >= (math.MinInt32>>1) && a <= (x<<1) {
      x <<= 1
      cnt <<= 1
    }
    ans += cnt
    a -= x
  }

  if sign {
    return ans
  }
  return -ans
}
function divide(a: number, b: number): number {
  if (b === 1) {
    return a;
  }
  if (a === -(2 ** 31) && b === -1) {
    return 2 ** 31 - 1;
  }

  const sign: boolean = (a > 0 && b > 0) || (a < 0 && b < 0);
  a = a > 0 ? -a : a;
  b = b > 0 ? -b : b;
  let ans: number = 0;

  while (a <= b) {
    let x: number = b;
    let cnt: number = 1;

    while (x >= -(2 ** 30) && a <= x << 1) {
      x <<= 1;
      cnt <<= 1;
    }

    ans += cnt;
    a -= x;
  }

  return sign ? ans : -ans;
}
public class Solution {
  public int Divide(int a, int b) {
    if (b == 1) {
      return a;
    }
    if (a == int.MinValue && b == -1) {
      return int.MaxValue;
    }
    bool sign = (a > 0 && b > 0) || (a < 0 && b < 0);
    a = a > 0 ? -a : a;
    b = b > 0 ? -b : b;
    int ans = 0;
    while (a <= b) {
      int x = b;
      int cnt = 1;
      while (x >= (int.MinValue >> 1) && a <= (x << 1)) {
        x <<= 1;
        cnt <<= 1;
      }
      ans += cnt;
      a -= x;
    }
    return sign ? ans : -ans;
  }
}
class Solution {
  /**
   * @param integer $a
   * @param integer $b
   * @return integer
   */

  function divide($a, $b) {
    if ($b == 0) {
      throw new Exception('Can not divide by 0');
    } elseif ($a == 0) {
      return 0;
    }
    if ($a == -2147483648 && $b == -1) {
      return 2147483647;
    }
    $sign = $a < 0 != $b < 0;

    $a = abs($a);
    $b = abs($b);
    $ans = 0;
    while ($a >= $b) {
      $x = $b;
      $cnt = 1;
      while ($a >= $x << 1) {
        $x <<= 1;
        $cnt <<= 1;
      }
      $a -= $x;
      $ans += $cnt;
    }

    return $sign ? -$ans : $ans;
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文