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)

所以只要计算出所有divisor*2^k,然后减去即可。

题目大意:

给定两个整数,要求不用乘除法和取模运算,计算出a/b的值,当结果越界的时候输出INT最大值

Java源代码(260ms):

public class Solution {
    public int divide(int dividend, int divisor) {
        long a,b,flag=0,sum=0;
        long[] map=new long[33],times=new long[33];
        if(dividend<0 && divisor<0)flag=1;
        else if(dividend>0 && divisor>0)flag=1;
        a=Math.abs((long)dividend);
        b=Math.abs((long)divisor);
        int i=0;
        map[0]=b;times[0]=1;
        while(map[i]<=a){
            i++;
            map[i]=map[i-1]+map[i-1];
            times[i]=times[i-1]+times[i-1];
        }
        for(int j=i-1;j>=0;j--){
            while(a >= map[j]){
                a-=map[j];
                sum+=times[j];
            }
        }
        sum=flag==1?sum:-sum;
        if(sum>Integer.MAX_VALUE || sum<Integer.MIN_VALUE)return Integer.MAX_VALUE;
        return (int)sum;
    }
}

C语言源代码(4ms):

long long ABS(long long a){
    return a>0?a:-a;
}
int divide(int dividend, int divisor) {
    int i=0,j,flag=0;
    long long sum=0,a,b,map[33],times[33],STOP=1;
    STOP=((long long)2147483647)+1;
    if(divisor==0)return INT_MAX;
    if(dividend==0)return 0;
    if((dividend>0 && divisor>0) || (dividend<0 && divisor<0))flag=1;
    a=ABS((long long)dividend);
    b=ABS((long long)divisor);
    map[0]=b;times[0]=1;
    while(map[i] <= a && i<33){
        i++;
        map[i]=map[i-1]+map[i-1];
        times[i]=times[i-1]+times[i-1];
    }
    for(j=i-1;j>=0;j--){
        while(a >= map[j]){
            a-=map[j];
            sum+=times[j];
        }
    }
    sum=flag?sum:-sum;
    if(sum<INT_MIN || sum > INT_MAX)return INT_MAX;
    return (int)sum;
}

C++源代码(24ms):

class Solution {
public:
    int divide(int dividend, int divisor) {
        int i=0,j,flag=0;
        long long sum=0,a,b,map[33],times[33],STOP=1;
        STOP=STOP<<31;
        if(divisor==0)return INT_MAX;
        if((dividend>0 && divisor>0) || (dividend<0 && divisor<0))flag=1;
        a=abs((long long)dividend);
        b=abs((long long)divisor);
        map[0]=b;times[0]=1;
        while(map[i] <= STOP){
            i++;
            map[i]=map[i-1]+map[i-1];
            times[i]=times[i-1]+times[i-1];
        }
        for(j=i-1;j>=0;j--){
            while(a >= map[j]){
                a-=map[j];
                sum+=times[j];
            }
        }
        sum=flag?sum:-sum;
        if(sum<INT_MIN || sum > INT_MAX)return INT_MAX;
        return (int)sum;
    }
};

Python源代码(72ms):

class Solution:
    # @param {integer} dividend
    # @param {integer} divisor
    # @return {integer}
    def divide(self, dividend, divisor):
        flag=0
        if dividend>0 and divisor>0:flag=1
        elif dividend<0 and divisor<0:flag=1
        if dividend==0:return 0
        dividend=abs(dividend)
        divisor=abs(divisor)
        map=[0 for i in range(33)]
        times=[1 for i in range(33)]
        i=0
        map[0]=divisor;times[0]=1
        while map[i]<=dividend:
            i+=1
            map[i]=map[i-1]+map[i-1]
            times[i]=times[i-1]+times[i-1]
        j=i-1;sum=0
        while j>=0:
            while map[j]<=dividend:
                dividend-=map[j]
                sum+=times[j]
            j-=1
        sum = -sum if flag==0 else sum
        if sum>2147483647:return 2147483647
        return sum
时间: 2024-09-30 15:37:42

LeetCode 29 Divide Two Integers (C,C++,Java,Python)的相关文章

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). 可以

Java [leetcode 29]Divide Two Integers

题目描述: Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT. 解题思路: 把除数表示为:dividend = 2^i * divisor + 2^(i-1) * divisor + ... + 2^0 * divisor.这样一来,我们所求的商就是各系数之和了,而每个系数都可以通过移位操作获得. 详细解说请参考:http:/

LeetCode 29 Divide Two Integers(两个整数相除)(*)

翻译 不用乘法.除法.取余操作,将两个数相除. 如果它溢出了,返回MAX_INT 原文 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 d

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. 思路:这个题算法上不是很难,但是通过率相当低,只有15%,果然,自己在写完之后,各种出错,而且错误不是算法上的错误,是各种边界值没有考虑到,很多溢出错误等.下面是具体代码,有详细注释. public class Solution { p

LeetCode 29 Divide Two Integers (不使用乘法,除法,求模计算两个数的除法)

题目链接: https://leetcode.com/problems/divide-two-integers/?tab=Description Problem :不使用乘法,除法,求模计算两个数的除法~ 除法运算:被除数中包含有多少个除数的计算 由于是int类型的除法,因此结果可能超过int的最大值,当超过int的最大值时输出int的最大值 另写除法函数,计算出除法的商. 首先判断出除法运算后的结果是正数还是负数. 之后需要将被除数和除数都变为正数,进行进一步计算 当被除数小于除数时,返回0

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*

From now on,I grade the questions I've done,* less means more difficult *** done by myself **need see answer,but I can reappear it *need see answer&hard to reappear 29. Divide Two Integers Medium 7003349FavoriteShare Given two integers dividend and d

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+

[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 题意: 实现除法,不能用乘.除和取模