LeetCode //C - 29. Divide Two Integers

29. Divide Two Integers

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: [ − 2 31 , 2 31 − 1 −2^{31}, 2^{31} − 1 231,2311]. For this problem, if the quotient is strictly greater than 2 31 − 1 2^{31} - 1 2311, then return 2 31 − 1 2^{31} - 1 2311, and if the quotient is strictly less than − 2 31 -2^{31} 231, then return − 2 31 -2^{31} 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:
  • − 2 31 < = d i v i d e n d , d i v i s o r < = 2 31 − 1 -2^{31} <= dividend, divisor <= 2^{31} - 1 231<=dividend,divisor<=2311
  • divisor != 0

From: LeetCode
Link: 29. Divide Two Integers


Solution:

Ideas:
  1. Handle Signs: Compute the sign of the result based on the signs of dividend and divisor.
  2. Use Absolute Values: Since we’re working with possible negative numbers, we convert everything into positive values for ease of calculation.
  3. Exponential Search: Use a method to subtract bigger chunks of the divisor from the dividend by shifting the divisor left (equivalent to multiplying the divisor by powers of 2) until we overshoot the dividend.
  4. Subtraction and Accumulation: Once the optimal subtraction chunk is identified, subtract it from the dividend and accumulate the count of how many times this chunk fits into the dividend.
  5. Overflow Handling: Make sure the result does not exceed the bounds defined by 32-bit integers.
Code:
int divide(int dividend, int divisor) {
    // Edge cases for overflow
    if (dividend == INT_MIN && divisor == -1) {
        return INT_MAX;
    }

    // Determine the sign of the result
    int sign = (dividend > 0) ^ (divisor > 0) ? -1 : 1;

    // Work with absolute values to avoid overflow issues
    long long ldividend = llabs((long long)dividend);
    long long ldivisor = llabs((long long)divisor);

    long long result = 0;

    // Perform the division using bit manipulation and subtraction
    while (ldividend >= ldivisor) {
        long long temp = ldivisor, multiple = 1;
        while (ldividend >= (temp << 1)) {
            temp <<= 1;
            multiple <<= 1;
        }
        ldividend -= temp;
        result += multiple;
    }

    // Apply the sign to the result
    result = sign * result;

    // Handle potential overflow
    if (result > INT_MAX) return INT_MAX;
    if (result < INT_MIN) return INT_MIN;

    return (int)result;
}

相关推荐

  1. MySQL商城数据表(20-29

    2024-04-24 08:22:04       29 阅读
  2. 4- <span style='color:red;'>29</span>

    4- 29

    2024-04-24 08:22:04      33 阅读
  3. 周报 | 24.4.22-24.4.28文章汇总

    2024-04-24 08:22:04       36 阅读
  4. LeetCode day29

    2024-04-24 08:22:04       72 阅读
  5. 【嵌入式开发】29

    2024-04-24 08:22:04       49 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-24 08:22:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-24 08:22:04       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-24 08:22:04       82 阅读
  4. Python语言-面向对象

    2024-04-24 08:22:04       91 阅读

热门阅读

  1. 关于上传自己本地项目到GitHub的相关命令

    2024-04-24 08:22:04       34 阅读
  2. Unity UI擦除效果

    2024-04-24 08:22:04       37 阅读
  3. Rust:遍历 BinaryHeap

    2024-04-24 08:22:04       36 阅读
  4. 使用Python实现语音识别与处理模型

    2024-04-24 08:22:04       32 阅读
  5. Rust 和 Go 哪个更好?

    2024-04-24 08:22:04       78 阅读
  6. 【MySql】MySQL查询中的笛卡尔积现象解析

    2024-04-24 08:22:04       44 阅读
  7. vscode 如何debug python torchrun deepspeed

    2024-04-24 08:22:04       111 阅读
  8. 4. HTTPS通信(握手)过程

    2024-04-24 08:22:04       37 阅读
  9. 无人机类型有哪些?

    2024-04-24 08:22:04       32 阅读