题目:Divide Two Integers

Divide two integers without using multiplication, division and mod operator.

If it is overflow, return MAX_INT.

看讨论区大神的思路:

In this problem, we are asked to divide two integers. However, we are not allowed to use division, multiplication and mod operations. So, what else can we use? Yeah, bit manipulations.

Let's do an example and see how bit manipulations work.

Suppose we want to divide 15 by 3, so 15 is dividend and 3 is divisor. Well, division simply requires us to find how many times we can subtract the divisor from the the dividend without making the dividend negative.

Let's get started. We subtract 3 from 15 and we get 12, which is positive. Let's try to subtract more. Well, we shift 3 to the left by 1 bit and we get 6. Subtracting 6 from 15 still gives a positive result. Well, we shift again and get 12. We subtract 12 from 15 and it is still positive. We shift again, obtaining 24 and we know we can at most subtract 12. Well, since 12 is obtained by shifting 3 to left twice, we know it is 4 times of 3. How do we obtain this 4? Well, we start from 1 and shift it to left twice at the same time. We add 4 to an answer (initialized to be0). In fact, the above process is like 15 = 3 * 4 + 3. We now get part of the quotient (4), with a remainder 3.

Then we repeat the above process again. We subtract divisor = 3 from the remaining dividend = 3 and obtain 0. We know we are done. No shift happens, so we simply add 1 << 0 to the answer.

Now we have the full algorithm to perform division.

According to the problem statement, we need to handle some exceptions, such as overflow.

Well, two cases may cause overflow:

  1. divisor = 0;
  2. dividend = INT_MIN and divisor = -1 (because abs(INT_MIN) = INT_MAX + 1).

Of course, we also need to take the sign into considerations, which is relatively easy.

Putting all these together, we have the following code.

class Solution {
public:
int divide(int dividend, int divisor) {
if (!divisor || (dividend == INT_MIN && divisor == -1))
return INT_MAX;
int sign = ((dividend < 0) ^ (divisor < 0)) ? -1 : 1;
long long dvd = labs(dividend);
long long dvs = labs(divisor);
int res = 0;
while (dvd >= dvs) {
long long temp = dvs, multiple = 1;
while (dvd >= (temp << 1)) {
temp <<= 1;
multiple <<= 1;
}
dvd -= temp;
res += multiple;
}
return sign == 1 ? res : -res;
}
};
 #include<iostream>
#include<limits>
using namespace std;
class Solution {
public:
int divide(int dividend, int divisor)
{
int sign = ((dividend > ) ^ (divisor > ) ? - : );
if (!divisor || (dividend==INT_MIN&&divisor==-))
return INT_MAX;
long long divid = labs(dividend), divis = labs(divisor);
long long res = ;
while (divid >= divis)
{
long long temp = divis,multi_time=;
while (divid >= (temp<<))
{
temp <<= ;
multi_time <<=;
}
divid -= temp;
res += multi_time;
}
return sign == ? res:-res;
}
};
int main()
{
Solution test;
int res = test.divide(, );
cout << res << endl;
return ;
}

Divide Two Integers leetcode的更多相关文章

  1. Divide Two Integers —— LeetCode

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

  2. Divide Two Integers leetcode java

    题目: Divide two integers without using multiplication, division and mod operator. 题解: 这道题我自己没想出来...乘除 ...

  3. 29. Divide Two Integers - LeetCode

    Question 29. Divide Two Integers Solution 题目大意:给定两个数字,求出它们的商,要求不能使用乘法.除法以及求余操作. 思路:说下用移位实现的方法 7/3=2, ...

  4. [LeetCode] Divide Two Integers 两数相除

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

  5. leetcode面试准备:Divide Two Integers

    leetcode面试准备:Divide Two Integers 1 题目 Divide two integers without using multiplication, division and ...

  6. [Leetcode][Python]29: Divide Two Integers

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 29: Divide Two Integershttps://oj.leetc ...

  7. 【一天一道LeetCode】#29. Divide Two Integers

    一天一道LeetCode系列 (一)题目 Divide two integers without using multiplication, division and mod operator. If ...

  8. LeetCode: Divide Two Integers 解题报告

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

  9. 【Leetcode】【Medium】Divide Two Integers

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

随机推荐

  1. Excel 如何按条件计数和按条件求和(如按月求和)

    1.使用SUMPRODUCT进行多条件计数语法:=SUMPRODUCT((条件1)*(条件2)*(条件3)* …(条件n))作用:统计同时满足条件1.条件2到条件n的记录的个数.实例:=SUMPROD ...

  2. tcp协议-http协议-time-wait-close-wait必知

     前言:  tcp四次挥手过程中,谁主动断开,谁有time_wait,被动断开一方会有close_wait time_wait:保持端口占用2mls~4min,避免对方还有一些tcp片发往这个端口,新 ...

  3. Myeclipse以及Genymotion工具的使用以及java后台开发小结

    1. 服务端的Servlet程序修改并保存后,需要重启tomcat服务器才能使其修改有效.重新部署web项目是没有什么卵用的. 2. servers选项卡若是移走了看不到,在window-show v ...

  4. TabCtrl的基本用法

    MFC TabCtrl控件的使用方法及思路 1.建立基于Dialog的应用程序 2.添加TabCtrl控件,并关联变量 3.建立TabCtrl中存放的Dialog子对话框若干,并关联类 4.在Tabc ...

  5. 自己练习读取写入txt

    读取文件中的内容生成一个list,然后修改list后再写会该文件文件中的格式是:AA,BB,CC,DDblist = []for line in open('a.txt'): blist.extend ...

  6. Hadoop HDFS编程 API入门系列之路径过滤上传多个文件到HDFS(二)

    不多说,直接上代码. 代码 package zhouls.bigdata.myWholeHadoop.HDFS.hdfs6; import java.io.IOException;import jav ...

  7. Oracle优化查询技巧

    1. WHERE子句中的连接顺序:Oracle采用自下而上的顺序解析WHERE子句,根据这个原理,表之间的连接必须写在其他WHERE条件之前, 那些可以过滤掉最大数量记录的条件必须写在WHERE子句的 ...

  8. 如何使用XproerUI库(WTL)-XproerUI界面库教程

    版权所有 2009-2015 荆门泽优软件有限公司 保留所有权利 产品首页:http://www.ncmem.com/apps/xproerui/index.asp 开发文档(SkinStudio): ...

  9. Visual Studio 换皮肤

    通过字体和颜色修改 Visual Studio 提供了修改配色的入口,你完全可以根据自己的喜好进行自定义,下面就通过该方法把编辑器背景设置成 “豆沙绿”. 选择 工具 / 选项 / 环境 / 字体和颜 ...

  10. VC++ WINDOWS自定义消息范围

    WINDOWS自定义消息WM_USER和WM_APP WM_USER常量是Windows帮助应用程序定义私有窗口类里的私有消息,通常使用WM_USER+一个整数值,但总值不能超过0x7FFF(十进制: ...