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

原创 2015年11月17日 20:04:53

翻译

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

如果它溢出了,返回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;
    }
}
版权声明:本文为 nomasp柯于旺 原创,未经许可严禁转载!欢迎访问我的博客:http://blog.csdn.net/nomasp 举报

相关文章推荐

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

Divide two integers without using multiplication, division and mod operator.If it is overflow, retur...

leetCode 29.Divide Two Integers (两整数相除) 解题思路和方法

Divide Two Integers  Divide two integers without using multiplication, division and mod operato...

我是如何成为一名python大咖的?

人生苦短,都说必须python,那么我分享下我是如何从小白成为Python资深开发者的吧。2014年我大学刚毕业..

【LeetCode-面试算法经典-Java实现】【029-Divide Two Integers(两个整数相除)】

【029-Divide Two Integers(两个整数相除)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Divide two integers without u...

LeetCode OJ 之 Divide Two Integers (两个整数相除)

题目: Divide two integers without using multiplication, division and mod operator. 求两个整数相除的结果,不能使用...

5.divide-two-integers(两个整数相除)

5.divide-two-integers(两个整数相除) 链接:http://www.lintcode.com/zh-cn/problem/divide-two-integers/ 将两个整数相除,...

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

Problem: Divide two integers without using multiplication, division and mod operator. If it ...

LeetCode(29)Divide Two Integers

题目内容 Divide two integers without using multiplication, division and mod operator. 题目分析 最容易的想到的办法,是把...

[LeetCode]29.Divide Two Integers

【题目】 Divide two integers without using multiplication, division and mod operator. If it is overfl...

leetcode:29. Divide Two Integers

描述: Divide two integers without using multiplication, division and mod operator.If it is overflow, r...

LeetCode 29. Divide Two Integers

LeetCode 29. Divide Two Integers C++位运算
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)