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,231−1. For this problem, if the quotient is strictly greater than 2 31 − 1 2^{31} - 1 231−1, then return 2 31 − 1 2^{31} - 1 231−1, 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<=231−1
  • 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:
c 复制代码
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;
}
相关推荐
天空'之城8 小时前
C 语言工业级通用组件手写 24:互补滤波
c语言·开发语言·互补滤波·嵌入式算法·工业级组件
小程故事多_8010 小时前
从A2C、TRPO、PPO到GRPO,强化学习策略梯度算法完整演进与大模型落地实战解析
人工智能·算法
2601_9561219711 小时前
二分算法(知识点+题目)
c++·算法
AndrewHZ14 小时前
【LLM技术全景】阶段总结:技术原理篇核心知识回顾
人工智能·深度学习·算法·语言模型·大模型·llm·芯片开发
小星星闪亮登场15 小时前
2026萌新联赛第三场-- (郑州轻工业大学)
数据结构·c++·经验分享·算法·贪心算法·排序算法·深度优先
冻柠檬飞冰走茶15 小时前
PTA基础编程题目集 7-8超速判断(C++语言实现)
开发语言·数据结构·c++·算法
玖玥拾16 小时前
LeetCode 88 合并两个有序数组
算法·leetcode
数据皮皮侠AI16 小时前
上市公司数字供应链金融指数(2010-2024)
大数据·人工智能·算法
stevenseahang16 小时前
C语言标准演化史:从K&R到GNU,谁才是正统?
c语言·gnu·可移植性·标准演化·ansic
小程故事多_8016 小时前
用GRPO算法重塑多智能体系统,从原理落地到复杂任务规划实战
人工智能·算法