关闭

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

标签: divideintegerint-maxdivisenddivisor
4011人阅读 评论(0) 收藏 举报
分类:

翻译

不用乘法、除法、取余操作,将两个数相除。

如果它溢出了,返回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 divisor) {
        if(!divisor) return INT_MAX;
        if(divisor == 1) return dividend;
        if(divisor == -1) {
            if(dividend == INT_MIN) return INT_MAX;
            else return -dividend;
        }

        bool s1 = dividend < 0;
        bool s2 = divisor < 0;

        unsigned int nom = s1 ? -dividend : dividend;
        unsigned int den = s2 ? -divisor : divisor;

        unsigned int rem = 0;
        unsigned int quot = 0;

        for(int i = 31; i >= 0; --i) {
            rem <<= 1;
            rem |= (nom >> i) & 1;
            if(rem >= den) {
                rem -= den;
                quot |= (1 << i);
            }
        }

        return s1^s2? -quot : quot;
    }
};

再来两个代码……(惭愧……)

public class Solution
{
    public int Divide(int dividend, int divisor)
    {
        //1. check overflow: 2 ways of over flow 1) 0 divisor; 2) int.Minvalue/(-1)
        if (divisor == 0 || dividend == int.MinValue && divisor == -1) return int.MaxValue;
        //2. calculate sign
        int sign = dividend > 0 ^ divisor > 0 ? -1 : 1, result = 0;
        long m = Math.Abs((long)dividend), n = Math.Abs((long)divisor);
        //3. looping from 1 to possible maximum pow(2, x) to add into result
        while (m >= n)
        {
            long subN = n;
            for (int subCount = 1; m >= subN; subCount <<= 1, subN <<= 1)
            {
                m -= subN;
                result += subCount;
            }
        }
        return result * sign;
    }
}
public class Solution
{
    public int Divide(int dividend, int divisor)
    {
        if (divisor == 1) return dividend;
        if (dividend == int.MinValue && divisor == -1 || divisor == 0) return int.MaxValue;
        int sign = (dividend < 0) ^ (divisor < 0) ? -1 : 1, result = 0;
        long dvd = Math.Abs((long)dividend), dvs = Math.Abs((long)divisor);
        while (dvd >= dvs)
        {
            long sub = dvs;
            int subR = 1;
            while (dvd >= (sub << 1))
            { sub <<= 1; subR <<= 1; }
            dvd -= sub;
            result += subR;
        }
        return sign * result;
    }
}
2
0
查看评论

[LeetCode-29] Divide Two Integers(两个整数相除,不用乘除取余算术符)

Divide two integers without using multiplication, division and mod operator.If it is overflow, return MAX_INT.采用减法代替除法 代码如下:/*方法一、用减法代替除法*/ /** divid...
  • xy010902100449
  • xy010902100449
  • 2015-09-15 19:01
  • 972

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: 不能乘除就...
  • runningtortoises
  • runningtortoises
  • 2015-05-13 09:27
  • 1820

leetcode 29 -- Divide Two Integers

Divide Two Integers 感想: >这道题因为有一点小问题一直导致AC不过,一会我会先贴上自己的正确代码,之前错误的贴到后面。 >在一直解决不了遇到的问题时,我去网上搜了下别人做这道题的思路,感觉自己的思路也算比较简单吧,就一直坚持自己的思路,依然不知道哪里错了,调...
  • wwh578867817
  • wwh578867817
  • 2015-06-12 17:23
  • 1727

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. 解题思路:这道题非常...
  • gx262091291
  • gx262091291
  • 2015-08-22 22:19
  • 1381

LeetCode29DivideTwoIntegers--In Java

主要采用的方法是:先不断通过移位操作找到divisor最接近dividend的倍数,如被除数是100,除数是3的话,这个倍数就是32。 然后用100减去3的32倍,这里的32倍是通过左移产生,并没有用到乘法。 这时,再用一个循环找到3的多少倍最接近于100和它32倍的差,即找到最接近于4的倍数,通过...
  • sinat_27564919
  • sinat_27564919
  • 2016-02-22 20:24
  • 757

[LeetCode29]Divide Two Integers

Divide two integers without using multiplication, division and mod operator. Analysis:
  • sbitswc
  • sbitswc
  • 2014-06-12 01:32
  • 7287

Divide Two Integers -- LeetCode

原题链接: http://oj.leetcode.com/problems/divide-two-integers/  这道题属于数值处理的题目,对于整数处理的问题,在Reverse Integer中我有提到过,比较重要的注意点在于符号和处理越界的问题。对于这道题目,因为不能用...
  • linhuanmars
  • linhuanmars
  • 2014-02-27 05:41
  • 26820

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. 这道题的要求是在不使...
  • makuiyu
  • makuiyu
  • 2015-02-02 21:58
  • 1059

leetcode 29 Divide Two Integers(整数相除)

不能使用乘法、除法、模运算来实现整数除法。
  • cyn618
  • cyn618
  • 2016-08-25 23:20
  • 118

LeetCode(29)Divide Two Integers

题目内容 Divide two integers without using multiplication, division and mod operator. 题目分析 最容易的想到的办法,是把除法转化为减法,就像把乘法转化为加法一样,提交后发现,这个做法超时了,比如遇到2147483647/...
  • feliciafay
  • feliciafay
  • 2014-01-03 07:24
  • 2576
    个人资料
    • 访问:1611042次
    • 积分:24254
    • 等级:
    • 排名:第327名
    • 原创:521篇
    • 转载:0篇
    • 译文:29篇
    • 评论:798条
    Billboard:
    微博
    博客专栏
    最新评论