otal Accepted: 54356 Total Submissions: 357733 Difficulty: Medium

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

If it is overflow, return MAX_INT.

1.除法转换为加法,加法转换为乘法,y个x相加的结果就是x*y.

假设结果为dividend的一半,用此值与divisor相乘,如果相乘结果大于dividend则,high=dividend,然后继续二分

/**
两个整数相除也可能会溢出,最小负数除以-1就溢出了
*/
class Solution {
public:
long long int multiply(long long x,long long int y)
{
if(y==){
return x;
}
long long int res = multiply(x,y>>);
res = (&y) ? (res<<) + x: (res<<);
return res;
} int divide(int dividend, int divisor) {
long long int l_dividend = fabs(dividend);
long long int l_divisor = fabs(divisor); if(l_dividend < l_divisor){
return ;
}
if((dividend==INT_MIN && divisor==-) || divisor==){
return INT_MAX;
} int sign = ((dividend>>)^(divisor>>)) ? -:; long long int low = ,high =l_dividend; while(low<=high){
long long int mid = low+((high-low)>>);
long long int res = multiply(l_divisor,mid);
if(res == l_dividend){
return mid*sign;
}else if(res<l_dividend){
low = mid+;
}else{
high = mid-;
}
} return (low-)*sign;
}
};
 
2.除法分配率:a/b = (x+y)/b = x/b+y/b ,其中a=x+y;
42/6;
dividend = 42,divisor=6;
42 = 6*2*2 + 18  ---> 42/6 = 24/6 + 18/6
18 = 6*2 + 6;     ----> 18/6 = 12/6 + 6
6 = 6+0;            ---->  6/6  = 6/6 + 0
所以:42/6 = 2*2+2+1=7;
 
/**
两个整数相除也可能会溢出,最小负数除以-1就溢出了
*/
class Solution {
public:
int divide(int dividend, int divisor) {
long long int l_dividend = fabs(dividend);
long long int l_divisor = fabs(divisor); if(l_dividend < l_divisor){
return ;
}
if((dividend==INT_MIN && divisor==-) || divisor==){
return INT_MAX;
} int sign = ((dividend>>)^(divisor>>)) ? -:;
int res = ;
while(l_dividend >= l_divisor){
long long int tmp = l_divisor;
int occur_times = ;
while(tmp <= l_dividend){
tmp = tmp<<;
occur_times++;
}
res += (<<(occur_times-));
l_dividend -= (tmp>>);
} return res*sign;
}
};
 

[Math]Divide Two Integers的更多相关文章

  1. Java for LeetCode 029 Divide Two Integers

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

  2. Java [leetcode 29]Divide Two Integers

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

  3. leetcode面试准备:Divide Two Integers

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

  4. Divide Two Integers 解答

    Question Divide two integers without using multiplication, division and mod operator. If it is overf ...

  5. 29. Divide Two Integers (JAVA)

    Given two integers dividend and divisor, divide two integers without using multiplication, division ...

  6. [LeetCode] 29. Divide Two Integers(不使用乘除取模,求两数相除) ☆☆☆

    转载:https://blog.csdn.net/Lynn_Baby/article/details/80624180 Given two integers dividend and divisor, ...

  7. Divide Two Integers leetcode java

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

  8. LeetCode: Divide Two Integers 解题报告

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

  9. [LeetCode] 29. Divide Two Integers ☆☆

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

随机推荐

  1. OpenCV——Mat,IplImage,CvMat类型转换

    Mat,cvMat和IplImage这三种类型都可以代表和显示图像,三者区别如下 Mat类型侧重于计算,数学性较高,openCV对Mat类型的计算也进行了优化. 而CvMat和IplImage类型更侧 ...

  2. Qt调用外部程序QProcess通信

    mainwindow.cpp文件: -------------------------------- #include "mainwindow.h" #include " ...

  3. Niagara AX之在Station下显示Home节点

    默认的Station下是没有Home节点的,那么,这个Home节点是怎么添加上去的呢? 注意Home后面的描述(Description):“Navigation tree defined by nav ...

  4. css white-space

    以下是对上面几个属性的测试效果如下: 具体代码如下: <!DOCTYPE html> <html lang="en"> <head> <m ...

  5. mysql----innodb统计信息

    对innodb 统计信息的控制可以通过如下几个常用的variables 来实现 1.innodb_stats_persistent: 这个参数控制着innodb的统计信息是否持久化到磁盘,先说明一下持 ...

  6. instancetype和id的区别

    一.什么是instancetype instancetype是clang 3.5开始,clang提供的一个关键字,表示某个方法返回的未知类型的Objective-C对象.我们都知道未知类型的的对象可以 ...

  7. C# 如何查看源程序的IL代码

    1.打开microsoft  visual  studio  2008  /  visual  studio  tools /  visual  studio  2008 命令提示  ,并输入ilda ...

  8. C# DateTime类,TimeSpan类

    DateTime类是.Net中用于处理时间类型数据的. 一.字段 MaxValue 表示 DateTime 的最大可能值.此字段为只读. MinValue     表示 DateTime 的最小可能值 ...

  9. SQL Server 2008空间数据应用系列十二:Bing Maps中呈现GeoRSS订阅的空间数据

    原文:SQL Server 2008空间数据应用系列十二:Bing Maps中呈现GeoRSS订阅的空间数据 友情提示,您阅读本篇博文的先决条件如下: 1.本文示例基于Microsoft SQL Se ...

  10. 点击Winform右下角图标,在最前端展示窗口

    //调用Windows API 展示窗口到最前端 SwitchToThisWindow(this.Handle, true);//窗体的句柄 this.Handle     SwitchToThisW ...