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. GridView+ZedGraph【转】

    edgraph图表控件的强大功能令人出乎意料,与OWC相比我想应该毫不逊色,近来需求要求作出相关数据统计,不想使用BI这类的强大东西,所以搜索到 了免费的开源的Zedgraph控件.使用起来也非常方便 ...

  2. 总结前端JQ常用的一些操作手法(慢慢完善)

    1.实例化Js一个object对象,把它当做类来用,事例是操作url的参数 function GetRequestCondition() { var url = window.location.hre ...

  3. IT学习方法

    IT 技术的发展日新月异,新技术层出不穷,具有良好的学习能力,能及时获取新知识.随时补充和丰富自己,已成为程序员职业发展的核心竞争力.本文中,作者结合多年的学习经验总结出了提高程序员学习能力的三个要点 ...

  4. pom.xml配置

    1:头部引用 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3 ...

  5. 秒(s) 毫秒(ms) 微秒(μs) 纳秒(ns) 皮秒(ps)及Java获得 .

    Date date=new Date(); long hm=date.getTime(); //获取毫秒 或者 毫秒级:System.currentTimeMillis() 纳秒级: System.n ...

  6. Html 编码 queryUrl = encodeURI(queryUrl);

    Html  编码 queryUrl = encodeURI(queryUrl);

  7. Git学习05 --分支管理02

    1.冲突 产生冲突后,查看readme.txt   可以看到冲突内容 <<<<<<< ======= >>>>>>> ...

  8. 十一、外观(Facade)模式--结构模式(Structural Pattern)

    外部与一个子系统的通信必须通过一个统一的门面(Facade)对象进行,这就是门面模式.门面模式要求一个子系统的外部与其内部的通信必须通过一个统一的门面(Facade)对象进行. 门面模式提供一个高层次 ...

  9. POP3、SMTP、IMAP和Exchange都是个什么玩意?

    很多时候一直对POP3.SMTP.IMAP和Exchange等迷迷糊糊的.下面就整理说明一下: 当前常用的电子邮件协议有SMTP.POP3.IMAP4,它们都隶属于TCP/IP协议簇,默认状态下,分别 ...

  10. C# Url编码 HtmlUrl编码

    今天看了Artwl的一片关于编码的文章,感觉写的非常好,而且人家那博客园的样式都比哥的好看得多,一幕了然,尤其是那黑色背景的H1,妈个B了,哥太喜欢了.既然如果,就来就着它的文章跟样式,顺便来总结一下 ...