【LeetCode】No.29. Divide Two Integers -- Java Version

本文介绍了两种不使用乘法、除法和取余运算进行整数除法的方法。第一种方法通过计算相减的次数得到商,第二种方法利用指数增加除数并求余,直至被除数小于除数。这两种方法都适用于32位整数范围,并处理了溢出情况。
摘要由CSDN通过智能技术生成

题目链接: https://leetcode.com/problems/divide-two-integers/

1. 题目介绍

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

【Translate】: 给定两个整数的被除数和除数,两个整数相除时不能使用乘法、除法和取余运算。

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.

【Translate】: 整数除法应该向零截断,这意味着失去小数部分。例如,8.345将被截断为8,-2.7335将被截断为-2。

Return the quotient after dividing dividend by divisor.

【Translate】: 返回商。

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.

【Translate】: 注意:假设我们处理的环境只能存储32位带符号整数范围内的整数:[231,231-1]。对于这个问题,如果商严格大于231 - 1,则返回231 - 1,如果商严格小于-231,则返回-231

【测试用例】:
test
【约束】:
Constraints

2. 题解

2.1 减法运算

  该题解的思想就是通过计算数字相减的次数,从而确定商。通过dividend < 0 ^ divisor < 0来确定最后的结果是否为负。

    public int divide(int dividend, int divisor) {
        // return (int) dividend/divisor;
        if (dividend == Integer.MIN_VALUE && divisor == -1) return Integer.MAX_VALUE; 
        int i = 0;
        boolean negative = dividend < 0 ^ divisor < 0;
        dividend = Math.abs(dividend);
        divisor = Math.abs(divisor);
        for (i = 0; dividend - divisor >= 0; i++)
        {
            dividend = dividend - divisor;
        }
        return negative? -i : i;
    }

case1

2.2

  pprabu49提供的题解Java | 0ms | 100% faster | Obeys all conditions

解题思想:

  1. 以指数方式增加除数,直到它超过被除数,然后用它减去。
  2. 将除数相加,求余数。
  3. 重复同样的操作,直到它变为 0
    example
    public int divide(int dividend, int divisor) {
        if (dividend == Integer.MIN_VALUE && divisor == -1) return Integer.MAX_VALUE; //Cornor case when -2^31 is divided by -1 will give 2^31 which doesnt exist so overflow 
         
        boolean negative = dividend < 0 ^ divisor < 0; //Logical XOR will help in deciding if the results is negative only if any one of them is negative
        
        dividend = Math.abs(dividend);
        divisor = Math.abs(divisor);
        int quotient = 0, subQuot = 0;
        
        while (dividend - divisor >= 0) {
            for (subQuot = 0; dividend - (divisor << subQuot << 1) >= 0; subQuot++);
            quotient += 1 << subQuot; //Add to the quotient
            dividend -= divisor << subQuot; //Substract from dividend to start over with the remaining
        }
        return negative ? -quotient : quotient;
    }

case2

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TomLazy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值