LeetCode 28 Divide Two Integers
Divide two integers without using multiplication, division and mod operator.
思路:1.先将被除数和除数转化为long的非负数,注意一定要为long。由于Integer.MIN_VALUE的绝对值超出了Integer的范围。
2.常理:不论什么正整数num都能够表示为num=2^a+2^b+2^c+...+2^n。故能够採用2^a+2^b+2^c+...+2^n来表示商,即dividend=divisor*(2^a+2^b+2^c+...+2^n),(a,b,c,....m互不相等。且最大为31,最小为0)。
而商的最大值为Integer.MIN_VALUE的绝对值。商最多有32个2的指数次相加。故时间复杂度为常数。
3.divisor*2^a用计算机表示为divisor<<a;
注意:若每次仅仅加一个divisor。则面对Integer.MAX_VALUE除以一个非常小的常数(eg:1。2。3),会超时。
public class Solution {
public int divide(int dividend, int divisor) { boolean positive = true;
if((dividend>0&&divisor<0)||(dividend<0&&divisor>0))
positive = false;
long did=dividend>=0?(long)dividend:-(long)dividend;
long dis=divisor>=0?(long)divisor:-(long)divisor; long quotients = positiveDivide(did, dis);
if (!positive)
return (int)-quotients;
return (int)quotients;
} public long positiveDivide(long did, long dis) {
long[] array = new long[32];
long sum = 0;
int i = 1;
long quotients = 0;
if(dis==1) return did;//为了避免-did=Integer.MIN_VALUE,而dis=1。出现故障
for (array[0]=dis; i < 32 && array[i - 1] <= did; i++)
array[i] = array[i - 1] << 1; for (i = i - 2; i >= 0; i--) {
if (sum <= did - array[i]) {
sum += array[i];
quotients += 1 << i;
}
}
return quotients;
}
}
优化版,减小内存的消耗。不申请动态数组
public class Solution {
public int divide(int dividend, int divisor) { boolean positive = true;
if((dividend>0&&divisor<0)||(dividend<0&&divisor>0))
positive = false;
long did=dividend>=0? (long)dividend:-(long)dividend;
long dis=divisor>=0?(long)divisor:-(long)divisor; long quotients = positiveDivide(did, dis);
if (!positive)
return (int)-quotients;
return (int)quotients;
} public long positiveDivide(long did, long dis) {
long sum = 0;
long quotients = 0;
if(dis==1) return did;//为了避免-did=Integer.MIN_VALUE,而dis=1。出现故障 //sum从divisor*2^31的開始加起,不能加则试试加上divisor*2^30。
//若不能则试试divisor*2^29,依此类推
for (int i = 31; i >= 0; i--) {
long temp=dis<<i;//该式为divisor*2^a //sum<=dividend则说明dividend大于divisor*(2^m+...+2^i),m最大为31
if (sum <= did - temp) {
sum += temp;
quotients += 1 << i;//2^i
}
}
return quotients;
}
}
LeetCode 28 Divide Two Integers的更多相关文章
- [LeetCode] 29. Divide Two Integers 两数相除
Given two integers dividend and divisor, divide two integers without using multiplication, division ...
- Java for LeetCode 029 Divide Two Integers
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- 【leetcode】Divide Two Integers (middle)☆
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- 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】Divide Two Integers
Divide two integers without using multiplication, division and mod operator. class Solution { public ...
随机推荐
- WinForm 之 使用ListView控件展示数据
在学习了这么多的WinForm基本控件后,今天在来学习一个比较有意思的包含图片的控件! >>>图像列表控件 ImageList是含有图像对象的集合,可以通过索引或关键字引用该集合中的 ...
- iOS- Size Class使用教程
1:它引入了一种新的概念,抛弃传统意义上我们适配时所谓的具体宽高尺寸,把屏幕的宽和高分别分成两种情况:Compact-紧凑, Regular-正常(Any-任意,其实就是这2种的组合,所以我没分成3种 ...
- 移动web——bootstrap如何修改原组件
基本介绍 1.bootstrap提供了丰富的组件,但是有时候我们不仅要删除不必要的标签,还需要修改里面的样式 2.我们建议若是修改样式那么最好将源样式从css中拷贝出来,名字换掉,然后修改具体样式,这 ...
- html5——3D转换
角度旋转 rotateX:默认以center绕x轴旋转 rotateY:默认以center绕y轴旋转 rotateZ:默认以cente绕z轴r旋转 //rotateX原点为center==>正值 ...
- CSS——background综合运用
搜索栏图标: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> < ...
- Python虚拟环境和requirements.txt文件的使用
参考: https://www.centos.bz/2018/05/centos-7-4-%E5%AE%89%E8%A3%85python3%E5%8F%8A%E8%99%9A%E6%8B%9F%E7 ...
- mysql zip版本如何安装
1.下载mysqlzip包并解压到D:\javadeveloper\mysql-5.6.24-winx642.配置环境变量在path中添加路径 D:\javadeveloper\mysql-5.6.2 ...
- CAD在网页中得到批注信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...
- zabbix实现163邮件报警
Zabbix 邮件报警 电脑登录网易邮箱配置,把自己的授权码看一下,并写入配置文件 server端安装配置邮件服务器 [root@server ~]# yum -y install mailx dos ...
- pandas.DataFrame.rank
原文:https://www.cnblogs.com/sunbigdata/p/7874581.html pandas.DataFrame.rank DataFrame.rank(axis=0 ...