# Untitled

unknown
java
8 months ago
1.9 kB
3
Indexable
Never
```class Solution
{
private int m_modifiedDividend;

public int inverseToPositive(int number)
{
if (number == Integer.MIN_VALUE)
{
number++;
}

if ((number >> 31 & 1) == 1)
{
return -number;
}

return number;
}

int recursive_solve(int dividend, int divisor, int result)
{
int temp_result = 1;
int temp_divisor = divisor;
while (temp_divisor << 1 <= dividend && temp_divisor << 1 > 0)
{
temp_divisor <<= 1;
temp_result += temp_result;
}

if (temp_result == 1)
{
m_modifiedDividend = dividend;

if (dividend >= divisor)
{
return result + 1;
}
return result;
}
result += temp_result;

return recursive_solve(dividend - temp_divisor, divisor, result);
}

public int divide(int dividend, int divisor)
{
if (dividend == Integer.MIN_VALUE && divisor == -1)
{
return Integer.MAX_VALUE;
}
if (divisor == 1)
{
return dividend;
}
if (divisor == Integer.MIN_VALUE && dividend != Integer.MIN_VALUE)
{
return 0;
}

int old_dividend = dividend;
int old_divisor = divisor;
dividend = inverseToPositive(dividend);
divisor = inverseToPositive(divisor);

int result = recursive_solve(dividend, divisor, 0);

if ((old_dividend < 0 || old_divisor < 0) && (old_dividend > 0 || old_divisor > 0))
{
result = -result;
if (old_dividend == Integer.MIN_VALUE && m_modifiedDividend + 1 == divisor << 1)
{
result -= 1;
}

return result;
}

return result;
}
}```