这道题目一直不会做,因为要考虑的corner case 太多。

1. divisor equals 0.

2. dividend equals 0.

3. Is the result negative?

4. when dividend equals Integer.MIN_VALUE and divisor equals -1, the result will overflow. convert result to long and then to integer.

5. have to use divided by divisor * 2 ^ n to avoid exceeding time limit

6. have to convert divisor and dividend to long to avoid overflow in shift.

7. constant 1 in java is compiled as integer by default.

public class Solution {
public int divide(int dividend, int divisor) {
if (divisor == 0) {
return dividend >= 0 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
}
if (dividend == 0) {
return 0;
}
boolean isNegative = (dividend > 0 && divisor < 0) || (dividend < 0 && divisor > 0);
long x = Math.abs((long)dividend);
long y = Math.abs((long)divisor);
long result = 0;
while (x >= y) {
int a = 0;
while (x >= (y << a)) {
a++;
}
a--;
result += (long)1 << a;
x -= y << a;
}
if (result == (long) Integer.MAX_VALUE + 1) {
return isNegative ? Integer.MIN_VALUE : Integer.MAX_VALUE;
}
return isNegative ? -(int)result : (int)result;
}
}

[leetcode] 29. divide two integers的更多相关文章

  1. [LeetCode] 29. Divide Two Integers 两数相除

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

  2. Java [leetcode 29]Divide Two Integers

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

  3. [leetcode]29. Divide Two Integers两整数相除

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

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

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

  5. [leetcode]29. Divide Two Integers 两整数相除

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

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

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

  7. [LeetCode]29. Divide Two Integers两数相除

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

  8. LeetCode 29 Divide Two Integers (不使用乘法,除法,求模计算两个数的除法)

    题目链接: https://leetcode.com/problems/divide-two-integers/?tab=Description   Problem :不使用乘法,除法,求模计算两个数 ...

  9. LeetCode: 29. Divide Two Integers (Medium)

    1. 原题链接 https://leetcode.com/problems/divide-two-integers/description/ 2. 题目要求 给出被除数dividend和除数divis ...

随机推荐

  1. 利用HTML5的History API实现无刷新跳转页面初探

    HTML4中的History API history这个东西大家应该都不陌生,我们经常使用history.back(-1)来实现后退功能,具体的属性和方法如下: 属性 length 历史的项数.Jav ...

  2. 收集的在线图片压缩(jpg/png)

    http://www.yasuotu.com/ http://www.jpegmini.com/ http://www.tumiaoya.com/ https://tinypng.com/(推荐) h ...

  3. R读取溢出的数据

    读取含多位数的数据 1(首选). install.packages("readxl")library(readxl) x<-read_excel("C:\\User ...

  4. 网络基础知识之 Ping

    ========================================假定主机A的IP地址是192.168.1.1,主机B的IP地址是192.168.1.2,都在同一子网内,则当你在主机A上 ...

  5. JS 原型的妙用

    原型是JS的一个重要的特征,通过它可以实现类和实例直接的继承关系. 1.原型来来实现数据备份 // 通过原型来来实现数据备份 function p(x){ this.x = x; } p.protot ...

  6. 获取页面内iframe里面的元素

    结构直接看图比较直接 这里window.frames获取的是iframe的数组 要记得给iframe加个ID才行 后面的记录会详细讲解 在父窗口获取页面内的iframe 里面的元素window.fra ...

  7. jquery 判断网络资源,网络文件是否存在

    前提是,不能跨域访问引入jquery库<script src="jQuery.js" type="text/javascript"></scr ...

  8. 原生JS常用代码汇总

    数组相关 var codes = new Array( ); //创建数组codes.length //数组长度 动态插入数组 codes.push(value);

  9. js闭包的作用域以及闭包案列的介绍:

    转载▼ 标签: it   js闭包的作用域以及闭包案列的介绍:   首先我们根据前面的介绍来分析js闭包有什么作用,他会给我们编程带来什么好处? 闭包是为了更方便我们在处理js函数的时候会遇到以下的几 ...

  10. Nginx反向代理+keepalived

    环境两台LB(nginx).两台web(nginx/apache都行) 安装httpd web01 [root@web01 /]# /etc/init.d/iptables stop iptables ...