LeetCode: 29. Divide Two Integers (Medium)
1. 原题链接
https://leetcode.com/problems/divide-two-integers/description/
2. 题目要求
给出被除数dividend和除数divisor,求出二者相除的商,余数忽略不计。
注意:不能使用乘法、除法和取余运算
3. 解题思路
陷阱一:MIN_VALUE/-1会溢出。因为Integer.MIN_VALUE = -的绝对值比Integer.MAX_VALUE大1
陷阱二:除数divisor不能等于“0”
思路一:使用一个while循环,当dividend >= divisor时,进入循环。dividend = divident - divisor,每减一次计数器res+1。循环结束后则得到二者之商。
缺点:时间复杂度为O( n ),当被除数很大、除数很小时,效率非常低
public class DivideTwoIntegers29 {
public static void main(String[] args) {
System.out.println(divided(-36, -3));
} public static int divide(int dividend, int divisor) {
if (divisor == 0 || dividend == Integer.MIN_VALUE && divisor == -1)
return Integer.MAX_VALUE;
int res = 0;
int sign = (dividend < 0) ^ (divisor < 0) ? -1 : 1; // 异或运算,除数和被除数同号为正,异号为负
long dvd = Math.abs((long) dividend);
long dvs = Math.abs((long) divisor);
while (dvd >= dvs) {
dvd -= dvs;
res++;
}
return sign == 1 ? res : -res;
}
}
思路二:采用位移运算,任何一个整数可以表示成以2的幂为底的一组基的线性组合,即num=a_0*2^0+a_1*2^1+a_2*2^2+...+a_n*2^n。基于以上这个公式以及左移一位相当于乘以2,我们先让除数左移直到大于被除数之前得到一个最大的基。然后接下来我们每次尝试减去这个基,如果可以则结果增加加2^k,然后基继续右移迭代,直到基为0为止。因为这个方法的迭代次数是按2的幂直到超过结果,所以时间复杂度为O(logn)。
public class DivideTwoIntegers29 {
public static void main(String[] args) {
System.out.println(divided(-36, -3));
} public static int divide(int dividend, int divisor) {
if (divisor == 0 || dividend == Integer.MIN_VALUE && divisor == -1)
return Integer.MAX_VALUE;
int res = 0;
int sign = (dividend < 0) ^ (divisor < 0) ? -1 : 1; // 异或运算,除数和被除数同号为正,异号为负
long dvd = Math.abs((long) dividend);
long dvs = Math.abs((long) divisor);
while (dvs <= dvd) {
long temp = dvs, mul = 1;
while (dvd >= temp << 1) { // temp<<1,二进制表示左移一位,等价于temp乘以2
temp = temp << 1;
mul = mul << 1;
System.out.println("temp = " + temp + " " + "mul = " + mul);
}
dvd -= temp;
System.out.println("dvd" + dvd);
res += mul;
}
return sign == 1 ? res : -res;
}
}
LeetCode: 29. Divide Two Integers (Medium)的更多相关文章
- [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
这道题目一直不会做,因为要考虑的corner case 太多. 1. divisor equals 0. 2. dividend equals 0. 3. Is the result negative ...
随机推荐
- 关于tcp状态及一些延展
1.常用的三个状态是:ESTABLISHED 表示正在通信,TIME_WAIT 表示主动关闭,CLOSE_WAIT 表示被动关闭. TCP协议规定,对于已经建立的连接,网络双方要进行四次握手才能成功断 ...
- codeforces 792C. Divide by Three
题目链接:codeforces 792C. Divide by Three 今天队友翻了个大神的代码来问,我又想了遍这题,感觉很好,这代码除了有点长,思路还是清晰易懂,我就加点注释存一下...分类吧. ...
- WebSphere集群环境修改IHS端口号的方法
参考资料:http://wenku.baidu.com/link?url=E9BkuEjJ16i9lg7l91L0-xhKCYkHV0mAnlwAeSlDCFM4TjZyk4ZVxmUu64BGd4F ...
- bzoj2816 [ZJOI2012]网络
Description http://www.lydsy.com/JudgeOnline/upload/zjoi2012.pdf 正解:$link-cut \ tree$. $LCT$板子题,直接维护 ...
- BIND简易教程(3):DNSSec配置
目录:BIND简易教程(1):安装及基本配置BIND简易教程(2):BIND视图配置BIND简易教程(3):DNSSec配置 (本篇) DNSSec,有个半英半中的名字叫DNS安全扩展.说的好听一点, ...
- xampp安装及使用时的问题总结
本文主要介绍易错点,具体安装过程可参考链接1 1.首先要以管理员身份运行,否则报错. 2.如果你的网站首页名字不是index,那么你在访问的时候就必须输入你首页的全称. 3.htdocs就是网站的根目 ...
- 自动化构建工具grunt的学习
关于grunt的一些记录,记的比较乱... 0.删除node_modules文件夹 命令行: npm install rimraf -g //先运行 rimraf node_modules //然后运 ...
- android 下使用Direct Texture
要使用Direct Texture,需要有一份android系统的源码 部分C++代码如下: #include <stdio.h> #include <stdlib.h> #i ...
- 在spring添加注解时,第一行package报错configure build path
练习spring的ioc的注解的时候写上注解就会在第一行package报错configure build path. 用的spring4.2.4的jar包.经过上网查阅资料,可能是jar包冲突,解决办 ...
- Office365学习笔记—Xslt自定义列表视图
1,在Office365中需要添加自定义的视图!用Spd添加视图,这儿我添加一个testView! (1)打开testView.aspx将</ZoneTemplate>节点中的内容全部删除 ...