Nav: << previous: 68.文本左右对齐 | next: 70.爬楼梯 >>


Description

tab: English
 
<p>Given a non-negative integer <code>x</code>, return <em>the square root of </em><code>x</code><em> rounded down to the nearest integer</em>. The returned integer should be <strong>non-negative</strong> as well.</p>
 
<p>You <strong>must not use</strong> any built-in exponent function or operator.</p>
 
<ul>
	<li>For example, do not use <code>pow(x, 0.5)</code> in c++ or <code>x ** 0.5</code> in python.</li>
</ul>
 
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
 
<pre>
<strong>Input:</strong> x = 4
<strong>Output:</strong> 2
<strong>Explanation:</strong> The square root of 4 is 2, so we return 2.
</pre>
 
<p><strong class="example">Example 2:</strong></p>
 
<pre>
<strong>Input:</strong> x = 8
<strong>Output:</strong> 2
<strong>Explanation:</strong> The square root of 8 is 2.82842..., and since we round it down to the nearest integer, 2 is returned.
</pre>
 
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
 
<ul>
	<li><code>0 &lt;= x &lt;= 2<sup>31</sup> - 1</code></li>
</ul>
 
 
 
> [!tip]- Hint 1
> 
> Try exploring all integers. (Credits: @annujoshi)
 
> [!tip]- Hint 2
> 
> Use the sorted property of integers to reduced the search space. (Credits: @annujoshi)
 
 
---
 
[submissions](https://leetcode.com/problems/sqrtx/submissions/) | [solutions](https://leetcode.com/problems/sqrtx/solutions/)
 
 
tab: 中文
 
<p>给你一个非负整数 <code>x</code> ,计算并返回&nbsp;<code>x</code>&nbsp;的 <strong>算术平方根</strong> 。</p>
 
<p>由于返回类型是整数,结果只保留 <strong>整数部分 </strong>,小数部分将被 <strong>舍去 。</strong></p>
 
<p><strong>注意:</strong>不允许使用任何内置指数函数和算符,例如 <code>pow(x, 0.5)</code> 或者 <code>x ** 0.5</code> 。</p>
 
<p>&nbsp;</p>
 
<p><strong>示例 1:</strong></p>
 
<pre>
<strong>输入:</strong>x = 4
<strong>输出:</strong>2
</pre>
 
<p><strong>示例 2:</strong></p>
 
<pre>
<strong>输入:</strong>x = 8
<strong>输出:</strong>2
<strong>解释:</strong>8 的算术平方根是 2.82842..., 由于返回类型是整数,小数部分将被舍去。
</pre>
 
<p>&nbsp;</p>
 
<p><strong>提示:</strong></p>
 
<ul>
	<li><code>0 &lt;= x &lt;= 2<sup>31</sup> - 1</code></li>
</ul>
 
 
 
> [!tip]- 提示 1
> 
> Try exploring all integers. (Credits: @annujoshi)
 
> [!tip]- 提示 2
> 
> Use the sorted property of integers to reduced the search space. (Credits: @annujoshi)
 
 
---
 
[提交记录](https://leetcode.cn/problems/sqrtx/submissions/) | [题解](https://leetcode.cn/problems/sqrtx/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