[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 ...
随机推荐
- 没有为 COM 互操作注册程序集 请使用 regasm.exe /tlb 注册该程序集——解决办法
错误现象: 错误 6 没有为 COM 互操作注册程序集“DevExpress.Utils.v13.1, Version=13.1.7.0, Culture=neutral, PublicKeyToke ...
- 【转】ubuntu vpn自动切换路由
需要的工作有以下三項 Ubuntu Network Manager Client (nmcli)用來建立VPN連線的工具其實在UBUNTU在桌面上就有VPN連線可以用, 為什麼我們還要這麼大費周章的用 ...
- Java实现的二分查找算法
二分查找又称折半查找,它是一种效率较高的查找方法. 折半查找的算法思想是将数列按有序化(递增或递减)排列,查找过程中采用跳跃式方式查找,即先以有序数列的中点位置为比较对象,如果要找的元素值小 于该中点 ...
- tensrflow python [defunct]
在ubuntu上面安装了GPU版本的tensorflow后,很容易碰到zombie thread 的问题,无法正常关闭tensorflow的线程,用ps aux|grep python可以看到 pyt ...
- pair correlation ggpair ggmatrix
https://zhuanlan.zhihu.com/p/23400450 首发于 R语言数据分析与可视化 关注专栏 登录 写文章 R 语言矩阵散点图 EasyCharts· 15 天前 散点 ...
- 介绍DSA数字签名,非对称加密的另一种实现
接下来我们介绍DSA数字签名,非对称加密的另一种实现. DSA DSA-Digital Signature Algorithm 是Schnorr和ElGamal签名算法的变种,被美国NIST作为DSS ...
- 基于canvas的陈列订货的分析
订货会软件中又新增了进行陈列订货,即一杆衣服订的显示出来,没订的不显示出来 主要遇到的问题是如何呈现,原先老是想着定位,left,top但是花出来的图容易出现原先的数据填写错误导致后期的图片的呈现出现 ...
- C++计算几何库
http://www.cgal.org/ http://shapeop.org/
- C语言基础(5)-有符号数、无符号数、printf、大小端对齐
1.有符号数和无符号数 有符号数就是最高位为符号位,0代表正数,1代表负数 无符号数最高位不是符号位,而就是数的一部分而已. 1011 1111 0000 1111 1111 0000 1011 10 ...
- Maven、SecureCRT使用问题汇集
1 Maven 无法下载pom文件中相关的依赖包 该问题可能有很多原因,我的原因是host中的localhost被修改了,改回来即可! 看起来好像出了一些网络原因的问题,顺着这个方向搜索,发现国外也有 ...