Divide Two Integers
来源:互联网 发布:尚客网络 编辑:程序博客网 时间:2024/05/21 17:11
Q:
Divide two integers without using multiplication, division and mod operator.
Solution:
public class Solution { public int divide(int dividend, int divisor) { if (divisor == 0) return Integer.MAX_VALUE; long first = dividend; long second = divisor; int result = 0; boolean positive = dividend>>31 == divisor>>31? true: false; first = first > 0? first: -first; second = second > 0? second: -second; int div = 0; while (second<<1 <= first) { second = second << 1; div++; } while (div >= 0) { if (first >= second) { first -= second; result += 1 << div; } else { second = second >> 1; div--; } } return positive? result: -result; }}
0 0
- LeetCode: Divide Two Integers
- [LeetCode]Divide Two Integers
- LeetCode: Divide Two Integers
- [LeetCode] Divide Two Integers
- Divide Two Integers
- Divide Two Integers
- LeetCode28:Divide Two Integers
- [Leetcode] Divide Two Integers
- LeetCode Divide Two Integers
- Divide Two Integers
- 【leetcode】Divide Two Integers
- Divide Two Integers
- Divide Two Integers
- [LeetCode]Divide Two Integers
- Divide Two Integers
- LeetCode-Divide Two Integers
- Divide Two Integers
- 25 - Divide Two Integers
- 4+2版本空间问题
- 第十二周 项目一 程序阅读 3-1
- CODE[VS] 1206
- 淘宝活动推广实战步骤教程阿里百站淘宝活动推广实战步骤教程
- 内存泄漏
- Divide Two Integers
- 品牌对于新的传播的理解
- 编译程序和解释程序的本质
- 在ios开发中用数据模型取代字典
- Xcode为什么干掉.pch文件若要使用怎么添加进去
- 圆角框练习(1)
- 数据库事务隔离级别
- Take ownership
- Ubuntu安装虚拟机XP系统 实现文件的共享