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. (转) Overloads and templates

    Overloaded functions In C++, two different functions can have the same name if their parameters are ...

  2. 使用dict和set

    Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也称为map,使用键-值(key-value)存储,具有极快的查找速度. d = {'Michael': 95, ...

  3. <正见>摘抄

    1- 没有全能的力量能够扭转死亡之路,因此也就不会困在期待之中.如果没有盲目的期待,就不会有失望,如果能够了解一切都是无常,就不会攀缘执著.如果不攀缘执著,就不会患得患失,也才能真正完完全全地活着. ...

  4. android的编译和运行过程深入分析

    android的编译和运行过程深入分析 作者: 字体:[增加 减小] 类型:转载 首先来看一下使用Java语言编写的Android应用程序从源码到安装包的整个过程,此过程对了解android的编译和运 ...

  5. nginx和apache的优缺点比较

    简单的说apache httpd和nginx都是web服务器,但两者适应的场景不同,也就是两者专注于解决不同的问题.apache httpd:稳定.对动态请求处理强,但同时高并发时性能较弱,耗费资源多 ...

  6. 做一个正气的杭电人--hdu2500

    做一个正气的杭电人 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  7. 凸包(hd1392)

    Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  8. Git工作中用法(Gitlab)

    感觉又有了新的认识.  一共有3个仓库,本地自己的,远程自己的,远程主仓库. 为了方便能及时从主仓库获取更新的内容要将远程主仓库也clone下来 git clone upstream url    / ...

  9. 巧用Graphviz和pvtrace等工具可视化C函数调用

    http://guiquanz.github.io/2012/10/15/linux_c_call_trace/

  10. springMVC+ freemark多视图配置

    <!--通用视图解析器--> <bean id="viewResolverCommon" class="org.springframework.web. ...