[leetcode] 29. divide two integers
这道题目一直不会做,因为要考虑的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的更多相关文章
- [LeetCode] 29. Divide Two Integers 两数相除
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
- Java [leetcode 29]Divide Two Integers
题目描述: Divide two integers without using multiplication, division and mod operator. If it is overflow ...
- [leetcode]29. Divide Two Integers两整数相除
Given two integers dividend and divisor, divide two integers without using multiplication, divisio ...
- [LeetCode] 29. Divide Two Integers(不使用乘除取模,求两数相除) ☆☆☆
转载:https://blog.csdn.net/Lynn_Baby/article/details/80624180 Given two integers dividend and divisor, ...
- [leetcode]29. Divide Two Integers 两整数相除
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
- [LeetCode] 29. Divide Two Integers ☆☆
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- [LeetCode]29. Divide Two Integers两数相除
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
- LeetCode 29 Divide Two Integers (不使用乘法,除法,求模计算两个数的除法)
题目链接: https://leetcode.com/problems/divide-two-integers/?tab=Description Problem :不使用乘法,除法,求模计算两个数 ...
- LeetCode: 29. Divide Two Integers (Medium)
1. 原题链接 https://leetcode.com/problems/divide-two-integers/description/ 2. 题目要求 给出被除数dividend和除数divis ...
随机推荐
- jQuery datatables
jQuery datatables 属性,用例 参考:http://datatables.club/example/ http://blog.csdn.net/mickey_miki/article/ ...
- Asynchronous fs.stat.isDirectory()
function showFile() { for(var i = 0; i< files.length; i++){ var itemFile = files[i]; fs.stat(__di ...
- 解决ArcGIS安装之后出现的Windows installer configures问题
----Please wait while Windows installer configures ArcGIS Desktop Error Message错误信息 When launching A ...
- thinkphp标签
1.volist标签 随后一条不一样的输出<volist name="pagehead" id="vo"> <if condition=&qu ...
- C++11的enum class & enum struct和enum
C++11的enum class & enum struct和enum C++标准文档--n2347(学习笔记) 链接:http://www.open-std.org/jtc1/sc22/wg ...
- 数据存储_ SQLite(3)
SQLite的应用 一.简单说明 1.在iOS中使用SQLite3,首先要添加库文件 libsqlite3.dylib 2.导入主头文件 #import <sqlite3.h> 二.具体说 ...
- css面包屑导航编号
content:counter(flag);counter-increment: flag;-->一般给导航条编号1,2,3
- 2015.4.25-2015.5.1 字符串去重,比例圆设计,中奖机和canvas橡皮擦效果等
1.字符串去重,html模板取值 2.javascript正则表达式之$1...$9 3.jquery插件 4.返回上一页并刷新 解决方法: <a href ="javas ...
- [Machine Learning & Algorithm] 随机森林(Random Forest)
1 什么是随机森林? 作为新兴起的.高度灵活的一种机器学习算法,随机森林(Random Forest,简称RF)拥有广泛的应用前景,从市场营销到医疗保健保险,既可以用来做市场营销模拟的建模,统计客户来 ...
- 《征服 C 指针》摘录5:函数形参 和 空的下标运算符[]
一.函数的形参的声明 C 语言可以像下面这样声明函数的形参: void func(int a[]) { // ... } 对于这种写法,无论怎么看都好像要向函数的参数传递数组. 可是,在 C ...