29. Divide Two Integers

原创 2016年08月30日 10:54:48

Divide two integers without using multiplication, division and mod operator.
If it is overflow, return MAX_INT.
基本思路:
不能用乘法,我们可以移位啊,移p位就相当于乘了2p
举例说明我的方法:
f(24,5)=24/5=20/5+4/5=4+0=4
5左移2位就是20,而左移3位就是40超过了24,因此第一部分商为22=4.
对余数4再进行分析,发现5不需要移位就比4大,因此第二部分商为0.
加和可得,商为4.

这道题的关键就在于边界的-2147483648如何处理,如果直接取绝对值会超过边界,所以要先把它转换成long long(或者__int64的形式),这样就方便处理,vc++6.0不支持long long(支持__int64),但是在leetcode上又只支持long long,所以给出两个版本,先是在vc上能通过的:

#include<iostream>
#include<math.h>
using namespace std;
class Solution {
public:
    int divide(int dividend, int divisor) 
    {
        if(0==divisor||(INT_MIN==dividend&&divisor==-1))
            return INT_MAX;//排除特殊情况
        __int64 x=fabs(dividend);
        __int64 y=fabs(divisor);
        int result=0;
        while(x>=y)
        {
            __int64 temp=y;
            __int64 count=1;
            while(temp<=x)
            {
                temp=temp<<1;
                count=count<<1;
            }
            result=result+(count>>1);
            x=x-(temp>>1);
        }
        if((dividend<0)^(divisor<0))
            result=-result;
        return result;
    }
};
int main()
{
    Solution solve;
    int x,y,result;
    cin>>x>>y;
    result=solve.divide(x,y);
    cout<<result<<endl;
    return 0;
}

改为long long

class Solution {
public:
    int divide(int dividend, int divisor) 
    {
        if(0==divisor||(INT_MIN==dividend&&divisor==-1))
            return INT_MAX;
        long long x=labs(dividend);
        long long y=labs(divisor);
        int result=0;
        while(x>=y)
        {
            long long temp=y;
            long long count=1;
            while(temp<=x)
            {
                temp=temp<<1;
                count=count<<1;
            }
            result=result+(count>>1);
            x=x-(temp>>1);
        }
        if((dividend<0)^(divisor<0))
            result=-result;
        return result;
    }
};

另外在leetcode上午讨论中看到一个很巧妙的方法
这里写图片描述

版权声明:本文为博主原创文章,未经博主允许不得转载。

[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 (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  Divide two integers without using multiplication, division and mod operato...

29. Divide Two Integers/49. Group Anagrams/96. Unique Binary Search Trees/560. Subarray Sum Equals K

Divide Two Integers Problem Description Implementation29. Divide Two IntegersProblem DescriptionDivi...

leetcode:29. Divide Two Integers

描述: Divide two integers without using multiplication, division and mod operator.If it is overflow, r...
  • Earl211
  • Earl211
  • 2017年03月13日 11:13
  • 187

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

翻译不用乘法、除法、取余操作,将两个数相除。如果它溢出了,返回MAX_INT原文Divide two integers without using multiplication, division a...
  • NoMasp
  • NoMasp
  • 2015年11月17日 20:04
  • 3837

Leetcode-29. Divide Two Integers

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

Leetcode29. Divide Two Integers

29. Divide Two Integers内容如下: Divide two integers without using multiplication, division and mod ope...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:29. Divide Two Integers
举报原因:
原因补充:

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