By Long Luo

This article is the solution 5 Approaches: BF use Long, BF use Int, Binary Search use Long, Binary Search use Int and Recursion of Problem 29. Divide Two Integers , Chinese version is 3种方法：暴力法，二分搜索，递归 .

Here shows 5 Approaches to slove this problem: Brute Force use $$\texttt{Long}$$, Brute Force use $$\texttt{Int}$$, Binary Search use $$\texttt{Long}$$, Binary Search use $$\texttt{Int}$$ and Recursion.

# Intuition

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

# Brute Force use Long

We can make the $$\textit{dividend}$$ and $$\textit{divisor}$$ positive, and cast to $$\texttt{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 Fast Power Algorithm: Binary Exponentiation , we can use the $$\texttt{quickAdd}$$ like the $$\texttt{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 $$\textit{divisor}$$ by increasing the $$\textit{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. 😉😃💗