Class Solution
java.lang.Object
g0001_0100.s0029_divide_two_integers.Solution
29 - Divide Two Integers.<p>Medium</p>
<p>Given two integers <code>dividend</code> and <code>divisor</code>, divide two integers <strong>without</strong> using multiplication, division, and mod operator.</p>
<p>The integer division should truncate toward zero, which means losing its fractional part. For example, <code>8.345</code> would be truncated to <code>8</code>, and <code>-2.7335</code> would be truncated to <code>-2</code>.</p>
<p>Return <em>the <strong>quotient</strong> after dividing</em> <code>dividend</code> <em>by</em> <code>divisor</code>.</p>
<p><strong>Note:</strong> Assume we are dealing with an environment that could only store integers within the <strong>32-bit</strong> signed integer range: <code>[\u22122<sup>31</sup>, 2<sup>31</sup> \u2212 1]</code>. For this problem, if the quotient is <strong>strictly greater than</strong> <code>[2<sup>31</sup> \u2212 1]</code>, then return <code>[2<sup>31</sup> \u2212 1]</code>, and if the quotient is <strong>strictly less than</strong> <code>[-2<sup>31</sup> \u2212 1]</code>, then return <code>[-2<sup>31</sup> \u2212 1]</code>.</p>
<p><strong>Example 1:</strong></p>
<p><strong>Input:</strong> dividend = 10, divisor = 3</p>
<p><strong>Output:</strong> 3</p>
<p><strong>Explanation:</strong> 10/3 = 3.33333.. which is truncated to 3.</p>
<p><strong>Example 2:</strong></p>
<p><strong>Input:</strong> dividend = 7, divisor = -3</p>
<p><strong>Output:</strong> -2</p>
<p><strong>Explanation:</strong> 7/-3 = -2.33333.. which is truncated to -2.</p>
<p><strong>Example 3:</strong></p>
<p><strong>Input:</strong> dividend = 0, divisor = 1</p>
<p><strong>Output:</strong> 0</p>
<p><strong>Example 4:</strong></p>
<p><strong>Input:</strong> dividend = 1, divisor = 1</p>
<p><strong>Output:</strong> 1</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>-2<sup>31</sup> <= dividend, divisor <= 2<sup>31</sup> - 1</code></li>
<li><code>divisor != 0</code></li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
divide
public int divide(int dividend, int divisor)
-