Algorithm
Problem Name: 29. Divide Two Integers
Problem Link: https://leetcode.com/problems/divide-two-integers/
Given two integers dividend
and divisor
, divide two integers without using multiplication, division, and mod operator.
The integer division should truncate toward zero, which means losing its fractional part. For example, 8.345
would be truncated to 8
, and -2.7335
would be truncated to -2
.
Return the quotient after dividing dividend
by divisor
.
Note: Assume we are dealing with an environment that could only store integers within the 32-bit signed integer range: [−231, 231 − 1]
. For this problem, if the quotient is strictly greater than 231 - 1
, then return 231 - 1
, and if the quotient is strictly less than -231
, then return -231
.
Example 1:
Input: dividend = 10, divisor = 3 Output: 3 Explanation: 10/3 = 3.33333.. which is truncated to 3.
Example 2:
Input: dividend = 7, divisor = -3 Output: -2 Explanation: 7/-3 = -2.33333.. which is truncated to -2.
Constraints:
-231 <= dividend, divisor <= 231 - 1
divisor != 0
Code Examples
#1 Code Example with Java Programming
Code -
Java Programming
class Solution {
private static int HALF_INT_MIN = -1073741824;
public int divide(int dividend, int divisor) {
if (dividend == Integer.MIN_VALUE && divisor == -1) {
return Integer.MAX_VALUE;
}
int negatives = 2;
if (dividend > 0) {
negatives--;
dividend = -dividend;
}
if (divisor > 0) {
negatives--;
divisor = -divisor;
}
int quotient = 0;
while (divisor >= dividend) {
int powerOfTwo = -1;
int value = divisor;
while (value >= HALF_INT_MIN && value + value >= dividend) {
value += value;
powerOfTwo += powerOfTwo;
}
quotient += powerOfTwo;
dividend -= value;
}
if (negatives != 1) {
return -quotient;
}
return quotient;
}
}
Copy The Code &
Try With Live Editor
Input
Output
#2 Code Example with Javascript Programming
Code -
Javascript Programming
const divide = function(dividend, divisor) {
if (!divisor || (dividend === Number.MIN_SAFE_INTEGER && divisor === -1)) {
return Number.MAX_SAFE_INTEGER;
}
const MAX_INT = Math.pow(2, 31) - 1;
if (dividend === -2147483648 && divisor === -1) return MAX_INT;
const sign = (dividend < 0) ^ (divisor < 0) ? -1 : 1;
let dvd = Math.abs(dividend);
let dvs = Math.abs(divisor);
let res = 0;
while (dvd >= dvs) {
let tmp = dvs;
let multiple = 1;
while (dvd >= tmp << 1 && tmp << 1 > 0) {
tmp <<= 1;
multiple <<= 1;
}
dvd -= tmp;
res += multiple;
}
return sign === 1 ? res : -res;
};
Copy The Code &
Try With Live Editor
Input
Output
#3 Code Example with Python Programming
Code -
Python Programming
class Solution:
def divide(self, dividend, divisor):
"""
:type dividend: int
:type divisor: int
:rtype: int
"""
positive = (dividend < 0) is (divisor < 0)
dividend, divisor, res = abs(dividend), abs(divisor), 0
while dividend >= divisor:
temp, i = divisor, 1
while dividend >= temp:
dividend -= temp
res += i
i <<= 1
temp <<= 1
if not positive: res = -res
return min(max(-2 ** 31, res), 2 ** 31 - 1)
Copy The Code &
Try With Live Editor
Input
Output
#4 Code Example with C# Programming
Code -
C# Programming
using System;
namespace LeetCode
{
public class _029_DivideTwoIntegers
{
public int Divide(int dividend, int divisor)
{
if (divisor == 0) { throw new DivideByZeroException(); }
uint a = dividend > 0 ? (uint)dividend : (uint)-dividend;
uint b = divisor > 0 ? (uint)divisor : (uint)-divisor;
uint result = 0, c = 0;
var index = 0;
while (a >= b)
{
c = b;
for (index = 0; a >= c && c != 0; index++, c *= 2)
{
a -= c;
result += (uint)1 << index;
}
}
return (dividend ^ divisor) >> 31 == -1
? (int)-result
: result > int.MaxValue ? int.MaxValue : (int)result;
}
}
}
Copy The Code &
Try With Live Editor
Input
Output