Nav: << previous: 28.找出字符串中第一个匹配项的下标 | next: 30.串联所有单词的子串 >>
Description
tab: English
<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>[−2<sup>31</sup>, 2<sup>31</sup> − 1]</code>. For this problem, if the quotient is <strong>strictly greater than</strong> <code>2<sup>31</sup> - 1</code>, then return <code>2<sup>31</sup> - 1</code>, and if the quotient is <strong>strictly less than</strong> <code>-2<sup>31</sup></code>, then return <code>-2<sup>31</sup></code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> dividend = 10, divisor = 3
<strong>Output:</strong> 3
<strong>Explanation:</strong> 10/3 = 3.33333.. which is truncated to 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> dividend = 7, divisor = -3
<strong>Output:</strong> -2
<strong>Explanation:</strong> 7/-3 = -2.33333.. which is truncated to -2.
</pre>
<p> </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>
---
[submissions](https://leetcode.com/problems/divide-two-integers/submissions/) | [solutions](https://leetcode.com/problems/divide-two-integers/solutions/)
tab: 中文
<p>给你两个整数,被除数 <code>dividend</code> 和除数 <code>divisor</code>。将两数相除,要求 <strong>不使用</strong> 乘法、除法和取余运算。</p>
<p>整数除法应该向零截断,也就是截去(<code>truncate</code>)其小数部分。例如,<code>8.345</code> 将被截断为 <code>8</code> ,<code>-2.7335</code> 将被截断至 <code>-2</code> 。</p>
<p>返回被除数 <code>dividend</code> 除以除数 <code>divisor</code> 得到的 <strong>商</strong> 。</p>
<p><strong>注意:</strong>假设我们的环境只能存储 <strong>32 位</strong> 有符号整数,其数值范围是 <code>[−2<sup>31</sup>, 2<sup>31 </sup>− 1]</code> 。本题中,如果商 <strong>严格大于</strong> <code>2<sup>31 </sup>− 1</code> ,则返回 <code>2<sup>31 </sup>− 1</code> ;如果商 <strong>严格小于</strong> <code>-2<sup>31</sup></code> ,则返回 <code>-2<sup>31</sup></code><sup> </sup>。</p>
<p> </p>
<p><strong>示例 1:</strong></p>
<pre>
<strong>输入:</strong> dividend = 10, divisor = 3
<strong>输出:</strong> 3
<strong>解释: </strong>10/3 = 3.33333.. ,向零截断后得到 3 。</pre>
<p><strong>示例 2:</strong></p>
<pre>
<strong>输入:</strong> dividend = 7, divisor = -3
<strong>输出:</strong> -2
<strong>解释:</strong> 7/-3 = -2.33333.. ,向零截断后得到 -2 。</pre>
<p> </p>
<p><strong>提示:</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>
---
[提交记录](https://leetcode.cn/problems/divide-two-integers/submissions/) | [题解](https://leetcode.cn/problems/divide-two-integers/solution/)
Solutions & Notes
properties:
note.updated:
displayName: Last Updated
note.relative_links:
displayName: Related Links
note.desc:
displayName: Description
note.grade:
displayName: Rating
note.program_language:
displayName: Language
note.time_complexity:
displayName: TC
note.space_complexity:
displayName: SC
views:
- type: table
name: Solutions & Notes
filters:
and:
- file.hasLink(this.file)
- file.tags.containsAny("leetcode/solution", "leetcode/note")
order:
- file.name
- desc
- program_language
- time_complexity
- space_complexity
- grade
- relative_links
- updated
sort:
- property: grade
direction: ASC
- property: time_complexity
direction: ASC
- property: program_language
direction: ASC
columnSize:
file.name: 104
note.space_complexity: 65
note.grade: 126
Similar Problems
properties:
note.lcTopics:
displayName: Topics
note.lcAcRate:
displayName: AC Rate
note.favorites:
displayName: Favorites
note.grade:
displayName: Rating
note.translatedTitle:
displayName: Title (CN)
note.lcDifficulty:
displayName: Difficulty
views:
- type: table
name: Similar Problems
filters:
and:
- file.hasLink(this.file)
- similarQuestions.contains(this.file)
order:
- file.name
- translatedTitle
- lcTopics
- lcDifficulty
- lcAcRate
- grade
- favorites
sort:
- property: file.name
direction: ASC
- property: lcTopics
direction: DESC
columnSize:
note.translatedTitle: 240
note.lcTopics: 347
note.lcAcRate: 75
note.grade: 122