[LeetCode] 029. Divide Two Integers (Medium) (C++/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql)

Github: https://github.com/illuz/leetcode


029. Divide Two Integers (Medium)

链接

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

代码(github):https://github.com/illuz/leetcode

题意

实现除法,不能用乘、除和取模。

分析

不能用乘、除和取模,那剩下的,还有加、减和位运算。

  1. 会想到的就是一次次去减,不过这样会超时。
  2. 在 1 的基础上优化下,跟快速幂一样,每次把除数翻倍(用位运算即可)。

这里有坑,就是结果可能超 int 范围,所以最好用 long long 处理,之后再转 int。

代码

C++:

class Solution {
public:
    int divide(int dividend, int divisor) {
		ll a = dividend >= 0 ? dividend : -(ll)dividend;
		ll b = divisor >= 0 ? divisor : -(ll)divisor;
		ll result = 0, c = 0;
		bool sign = (dividend > 0 && divisor < 0) ||
			(dividend < 0 && divisor > 0);

		while (a >= b) {
			c = b;
			for (int i = 0; a >= c; i++, c <<= 1) {
				a -= c;
				result += (1<<i);
			}
		}
		if (sign) {
			return max((ll)INT_MIN, -result);
		} else {
			return min((ll)INT_MAX, result);
		}
    }
};

Python:

class Solution:
    # @return an integer
    def divide(self, dividend, divisor):
        sign = (dividend < 0 and divisor > 0) or (dividend > 0 and divisor < 0)
        a, b = abs(dividend), abs(divisor)
        ret, c = 0, 0

        while a >= b:
            c = b
            i = 0
            while a >= c:
                a -= c
                ret += (1<<i)
                i += 1
                c <<= 1

        if sign:
            ret = -ret
        return min(max(-2147483648, ret), 2147483647)
时间: 2024-08-07 22:49:34

[LeetCode] 029. Divide Two Integers (Medium) (C++/Python)的相关文章

LeetCode 029 Divide Two Integers

题目要求:Divide Two Integers Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT. 分析: 不能用乘.除和取余,则只能用减了…… 代码如下: class Solution { public: int divide(int dividend, int divisor) { // 当 dividend = INT

Java for LeetCode 029 Divide Two Integers

Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT. 解题思路: 既然不呢个用乘除和取模运算,只好采用移位运算,可以通过设置一个length代表divisor向左的做大移位数,直到大于dividend,然后对length做一个循环递减,dividend如果大于divisor即进行减法运算,同时result加上对应的值,注意边界条

LeetCode: 29. Divide Two Integers (Medium)

1. 原题链接 https://leetcode.com/problems/divide-two-integers/description/ 2. 题目要求 给出被除数dividend和除数divisor,求出二者相除的商,余数忽略不计. 注意:不能使用乘法.除法和取余运算 3. 解题思路 陷阱一:MIN_VALUE/-1会溢出.因为Integer.MIN_VALUE = -的绝对值比Integer.MAX_VALUE大1 陷阱二:除数divisor不能等于"0" 思路一:使用一个wh

LeetCode --- 29. Divide Two Integers

题目链接:Divide Two Integers Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT. 这道题的要求是在不使用乘法.除法.取模运算的前提下实现两个整数相除.如果溢出,返回MAX_INT. 这道题的直接思路是用被除数不断减去除数,直到为0.这种方法的迭代次数是结果的大小,即比如结果为n,算法复杂度是O(n). 可以

LeetCode 28 Divide Two Integers

Divide two integers without using multiplication, division and mod operator. 思路:1.先将被除数和除数转化为long的非负数,注意一定要为long,因为Integer.MIN_VALUE的绝对值超出了Integer的范围. 2.常理:任何正整数num都可以表示为num=2^a+2^b+2^c+...+2^n,故可以采用2^a+2^b+2^c+...+2^n来表示商,即dividend=divisor*(2^a+2^b+

029 Divide Two Integers

029 Divide Two Integers class Solution: # @param {integer} dividend # @param {integer} divisor # @return {integer} def divide(self, dividend, divisor): intMax, intMin = 2147483647, -2147483648 if dividend==0 or divisor == 0: return 0 isNeg = (dividen

LeetCode 29 Divide Two Integers (C,C++,Java,Python)

Problem: Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT. Solution: 不能乘除就加减就行了,但是一个问题是加减有可能速度太慢,因此需要转换,由于任何一个数都能表示成二进制,所以有dividend=divisor*(a*2^1 + b*2^2 + ...... + m*2^k) 所以只要计算出所有diviso

leetcode之Divide Two Integers

Divide Two Integers Divide two integers without using multiplication, division and mod operator. 分析:不用乘.除.模运算实现除法.由于每个整数都可以写成an*2^n + --+a0*2^0的形式,所以可以使用移位来求商,首先是递归算法,此算法在leetcode上超时,不过理解起来比较简单,思路也是正确的. class Solution { public: int __div(int x,int y)

【leetcode】Divide Two Integers (middle)☆

Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT. 思路: 尼玛,各种通不过,开始用纯减法,超时了. 然后用递归,溢出了. 再然后终于开窍了,用循环,把被除数每次加倍去找答案,结果一遇到 -2147483648 就各种不行, 主要是这个数一求绝对值就溢出了. 再然后,受不了了,看答案. 发现,大家都用long long来解决溢