Title Description:
Divide-integers without using multiplication, division and mod operator.
If It is overflow, return max_int.
Problem Solving Ideas:
The divisor is expressed as: dividend = 2^i * divisor + 2^ (i-1) * divisor + ... + 2^0 * divisor. In this way, the quotient we seek is the sum of the coefficients, and each coefficient can be obtained by means of a shift operation.
For detailed explanation, please refer to: http://blog.csdn.net/whuwangyi/article/details/40995863
The code is as follows:
public class Solution {public int divide (int dividend, int divisor) {Boolean flag = (Dividend > 0 && Divis or > 0) | | (Dividend < 0 && Divisor < 0); long absdividend = Math.Abs ((long) dividend); Long absdivisor = Math.Abs ((long) divisor); Long quotient = Dividepositive (Absdividend, absdivisor); if (flag && quotient > Integer.max_value) Return Integer.max_value;return flag? (int) Quotient:-(int) quotient;} Public long dividepositive (long dividend, long divisor) {if (Dividend < divisor) return 0;long quotient = 1;long Origina Ldivisor = Divisor;while (Dividend >= (divisor << 1)) {quotient <<= 1;divisor <<= 1;} Return quotient + dividepositive (dividend-divisor, originaldivisor);}}
Java [Leetcode 29]divide-integers