LeetCode - 29. Divide Two Integers
来源:互联网 发布:怎么设置软件锁 编辑:程序博客网 时间:2024/05/17 09:28
这道题目要求我们在不适用乘除和模运算的情况先实现两个数的除法,那么这种情况下我们自然就会想到使用bit manipulation,我们来思考一下应该怎样使用bit manipulation:
假设对于15和3的除法,其中15是被除数,3是除数。除法的本质就是计算在被除数当中有多少个除数,所以我们首先用15 - 3 = 12,结果大于3,那我们接下来就减去更多。因为不能使用乘法,所以我们将3进行左移,3 << 1,相当于3 * 2,此时12 - 6 = 6仍然大于3,接着我们再把除数左移,得到24,此时15小于24。因为12是将3左移两次得到的,而12 = 3 * 4,所以我们想要的到这个4,这样就可以记录下被除数中到底有多少个除数,这里我们从1开始,每当需要把除数左移的时候,我们同样将1左移,这样左移两次后得到的结果就是4。上面的过程也就相当于15 = 3 * 4 + 3,这时我们已知的是被除数15中含有4个除数和一个3。
重复上面的过程,我们用被除数3减去除数3,得到0,这时我们的算法已经完成,只需要将结果加上1即可。
根据题意,我们还要处理溢出的情况,在这道题目中,溢出有可能在两种情况下出现:
1. 除数 = 0
2. 被除数为int的最小值,而此时除数为-1,因为Math.abs(Integer.MIN_VALUE) = Math.abs(Integer.MAX_VALUE) + 1
整体的代码如下:
public class Solution { public int divide(int dividend, int divisor) { if(dividend == 0) return 0; // corner case // Handle overflow if(divisor == 0 || (dividend == Integer.MIN_VALUE && divisor == -1)){ return Integer.MAX_VALUE; } int sign = ((dividend < 0) ^ (divisor < 0)) ? -1 : 1; long dvd = Math.abs((long)dividend); long dvs = Math.abs((long)divisor); int result = 0; // Iteratively dvs * 2 and subtract it from dvd while(dvd >= dvs){ long temp = dvs; int multiple = 1; while(dvd >= (temp << 1)){ multiple <<= 1; // record # of dvs temp <<= 1; // temp * 2 } dvd -= temp; result += multiple; } return sign < 0 ? -result : result; }}
知识点:
1. 记住这种用位运算实现除法的算法
2. 记下在除法中可能会出现溢出的两种情况,除数为0,或者在被除数为int最小值的情况下除数为-1
0 0
- [LeetCode]29.Divide Two Integers
- LeetCode --- 29. Divide Two Integers
- LeetCode 29.Divide Two Integers
- [Leetcode] 29. Divide Two Integers
- 【leetcode】29. Divide Two Integers
- *LeetCode 29. Divide Two Integers
- leetcode 29. Divide Two Integers
- leetcode 29. Divide Two Integers
- [LeetCode]29. Divide Two Integers
- LeetCode 29. Divide Two Integers
- LeetCode 29. Divide Two Integers
- leetcode 29. Divide Two Integers
- LeetCode-29.Divide Two Integers
- leetcode 29. Divide Two Integers
- leetcode 29. Divide Two Integers
- [leetcode]29. Divide Two Integers
- leetcode 29. Divide Two Integers
- LeetCode - 29. Divide Two Integers
- String/StringBuffer/StringBuilder详细对比
- 输出文件和原文件大小不同
- CSS-Float全解析
- Oauth2.0 获得access_token以后
- 002——struts的文件上传功能
- LeetCode - 29. Divide Two Integers
- java垃圾回收机制分析
- 319. Bulb Switcher
- Python入门:Django错误(1146,Table 'blog.django_session' doesn't exist")
- MySQL 加锁处理分析
- python模块之os模块
- GaoJueYi 【2016.07.13初中部 NOIP普及组 】模拟赛总结
- Facebook开源闪光控件(ShimmerForAndroid)使用
- Seckill系统高并发优化