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. SQL Server事务的存储过程

    在酒店管理系统开发中,我们会创建房间表和房间类型表(房型表)这两个表,如下图所示: 房型表:RoomType 房间表:Room 首先这两个表的关系:Room是从表,RoomType是主表,两表有主外键 ...

  2. alarm函数可以定时

    貌似是可以的,不过感觉好像这样用不是很好,最好还是用回timer_settimer一些列函数吧,不过既然开了头,就看下alarm怎么用吧. 1. 所需头文件  #include<unistd.h ...

  3. mysql 数据库连接池

    hibernate配置C3P0详解         分类:             hibernate              2013-08-14 16:16     1213人阅读     评论 ...

  4. 使用Promise规定来处理ajax请求的结果

    ajax()返回结果是成功的,调用done()中的回调函数: 失败则调用fail()中的回调函数; always()的回调函数不管成功是否都会调用: 可以是使用then()函数代替done()和fai ...

  5. JQuery中的省市联动

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  6. Android SqLite升级

    android开发中,如果大家使用到了sqlite就会牵涉到它的升级问题,因为升级后的表结构可能完全不一样,会有字段的添加或者删除等..    sqlite升级思路:      1:将表A重新命名:例 ...

  7. mysqldump 的方式来搭建master-->slave 的复制架构

    1.master 上要满足的最小条件: 1.server_id 已经设置成了一个非0值 2.log_bin 配置好binlog 2.slave 上要满足的最小条件 1.server_id 已经设置成了 ...

  8. abiword Namespace List

    abiword Namespace List Here is a list of all namespaces with brief descriptions: abicollab  这个命名空间以及 ...

  9. git diff 使用

    1. 本地工作目录与远程仓库对比(所有改动过的文件) git diff HEAD  (HEAD指向最新一次的提交,即最新版本) 2. 之对比给定的文件 git diff -- filename //是 ...

  10. DeflateStream类

    DeflateStream是另外一种压缩与解压缩流,使用方法与GZipStream类似,而且压缩之后的带下也差不多. 一.属性 BaseStream 获取对基础流的引用. CanRead  获取一个值 ...