Divide Two Integers
来源:互联网 发布:51单片机串口程序 编辑:程序博客网 时间:2024/04/28 22:40
Divide two integers without using multiplication, division and mod operator.
dividend = divisor*2^0 + divisor*2^1 + ... + divisor*2^n + mod; (mod<divisor).
public class Solution { public int divide(int dividend, int divisor) {// Start typing your Java solution below// DO NOT write main() functionif(dividend == 0 || divisor == 0)return 0;int sign = 1;if(dividend < 0)sign = -sign;if(divisor < 0)sign = -sign;long dd;long dr;if(dividend == Integer.MIN_VALUE)dd = (long)Integer.MAX_VALUE + 1;elsedd = Math.abs(dividend);if(divisor == Integer.MIN_VALUE)dr = (long)Integer.MAX_VALUE + 1;elsedr = Math.abs(divisor);long count = 1;long num = dr;while(num < dd){num <<= 1;count <<= 1;}int result = 0;while(num >= dr){while(dd >= num){dd -= num;result += count;}num >>= 1;count >>= 1;}if(sign < 0)return -result;elsereturn result;}}
- 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
- 什么是私有IP
- stasm 对齐关键源码
- 程序员技术练级攻略
- UITableViewCell 自定义默认选中 和 改变cell背景图片
- C++设计模式实现--单例
- Divide Two Integers
- 2013年5月21日 23:20:12
- Android 通信--蓝牙
- 智能指针之 shared_ptr
- SEAndroid概述
- RQNOJ 合唱队形
- SQL 性能调优 笔记
- 设计模式-建造者模式
- 12306分析