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 ...
随机推荐
- drupal 8——在CKEditor中导入video media时添加caption会导致video缩小至消失
在CKEditor中,我点击media browser,选择video型的media,并在caption中输入video的名字.当我保存后发现在前台页面的video消失了,只留下video的名字,点击 ...
- JS高级——扩展内置对象的方法
基本概念 内置对象有很多,几个比较重要的:Math.String.Date.Array 基本使用 1.内置对象创建出来的对象使用的方法使用的其实都是内置对象的原型对象中的方法 (1)a并没有charA ...
- VHDL之Serial-Parallel Multiplier
1 Serial-parallel multiplier Figure 12.1 shows the RTL diagram of a serial-parallel multiplier. One ...
- 使用NSSM将Kibana安装为Windows服务
Kibana不同于Elasticsearch,前者官方并没有提供安装为系统服务的方法,如果直接运行在生产环境中会尤为麻烦,一旦服务器因故重启就要手动开启,所以将Kibana安装为系统服务非常有必要. ...
- pycharm执行代码可以跑,但放到linux跑就报文件找不到
代码中包含当前路径 使用pycharm执行python,当前路径就是pycharm项目所在的路径,所以不会报错 但使用shell执行python,当前路径就会从shell所在的路径去找文件,所以找不到 ...
- Centos7安装gitlab服务器
1.先按照官方教程 https://about.gitlab.com/downloads/#centos7 大概内容如下: 1. Install and configure the necessary ...
- CentOS安装Nodejs-v8.11.1
(1)到NodeJs官网(https://nodejs.org/en/download/),复制下载链接 (2)下载并解压 命令:wget https://nodejs.org/dist/v8.11. ...
- Python培训时长多久可以学会?马哥教育9年经验之谈
在Python成为一门炙手可热的语言之后,很多人也开始准备向这个行业发展.技术入行也就是培训和自学两条路,各有优劣,不过培训因为学习比较系统比较快也受到不少人欢迎. 今天我就来给大家分享一下Pytho ...
- CF51F Caterpillar (边双+树形DP)
题目传送门 题目大意:给你一张n个点m条边的图.每次操作可以把两个点合并成一个(与之相连的边也都要连到新点上).求把图中每个联通块都变成“毛毛虫”的最小操作次数.“毛毛虫”必须是一棵树(可以存在自环) ...
- Ajax基本写法
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...