By Long Luo

This article is the solution of Problem 29. Divide Two Integers , Chinese version is 3种方法：暴力法，二分搜索，递归 .

Here shows 5 Approaches to slove this problem: BF use long, BF use int, Binary Search use Long, Binary Search use Int, Recursion.

# Intuition

To divide two integers without using multiplication, division, and mod operator, so we can subtract the divisor from the dividend util the $\textit{result} \ge 0$.

# Brute Force use Long

We can make the dividend and divisor positive, and cast to long, then process.

The solution will Time Limit Exceeded, we have to deal with some corner cases.

## Analysis

• Time Complexity: $O(x / y)$.
• Space Complexity: $O(1)$.

# Brute Force use Int

Since the environment that could only store integers within the 32-bit signed integer, so we have to deal with it.

## Analysis

• Time Complexity: $O(x / y)$.
• Space Complexity: $O(1)$.

# Binary Search use Long

Refer to
Illustration of Fast Power Algorithm - Exponentiation by Squaring or Binary Exponentiation , we can use the quickAdd like the quickMul.

## Analysis

• Time Complexity: $O(logx \times logy)$.
• Space Complexity: $O(1)$.

# Binary Search use Int

1. the corner cases;
2. Record the sign of the final result and turn both numbers to negative numbers;
3. Approximate the divisor by increasing the dividend incrementally.

## Analysis

• Time Complexity: $O(logx \times logy)$.
• Space Complexity: $O(1)$.

# Recursion

The recurison method.

## Analysis

• Time Complexity: $O(logx \times logy)$.
• Space Complexity: $O(1)$.

All suggestions are welcome.
If you have any query or suggestion please comment below.
Please upvote👍 if you like💗 it. Thank you:-)

Explore More Leetcode Solutions. 😉😃💗