{
  "type": "code_options",
  "author": "csdn.net",
  "source": "solution.md",
  "exercise_id": "1a3c7b78b2934efd89a842dfa71f79ff",
  "keywords": "数学,二分查找",
  "title": "x 的平方根",
  "desc": [
    {
      "content": "\n<p>实现&nbsp;<code>int sqrt(int x)</code>&nbsp;函数。</p>\n<p>计算并返回&nbsp;<em>x</em>&nbsp;的平方根，其中&nbsp;<em>x </em>是非负整数。</p>\n<p>由于返回类型是整数，结果只保留整数的部分，小数部分将被舍去。</p>\n<p><strong>示例 1:</strong></p>\n<pre><strong>输入:</strong> 4<strong><br />输出:</strong> 2</pre>\n<p><strong>示例 2:</strong></p>\n<pre><strong>输入:</strong> 8<strong><br />输出:</strong> 2<strong><br />说明:</strong> 8 的平方根是 2.82842..., 由于返回类型是整数，小数部分将被舍去。</pre>",
      "language": "markdown"
    }
  ],
  "answer": [
    {
      "content": "",
      "language": "java"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "java"
      }
    ],
    [
      {
        "content": "",
        "language": "java"
      }
    ],
    [
      {
        "content": "",
        "language": "java"
      }
    ]
  ],
  "template": {
    "content": "class Solution {\n\tpublic int mySqrt(int x) {\n\t\tint left = 0, right = 46340;\n\t\twhile (left < right) {\n\t\t\tint mid = (left + right) / 2;\n\t\t\tif (mid * mid < x)\n\t\t\t\tleft = mid + 1;\n\t\t\telse if (mid * mid > x)\n\t\t\t\tif ((mid - 1) * (mid - 1) <= x)\n\t\t\t\t\treturn mid - 1;\n\t\t\t\telse\n\t\t\t\t\tright = mid - 1;\n\t\t\telse\n\t\t\t\treturn mid;\n\t\t}\n\t\tif (left * left > x)\n\t\t\treturn left - 1;\n\t\treturn left;\n\t}\n}\n",
    "language": "java"
  },
  "node_id": "dailycode-cd10c107bc354eef83e1b81a46b78db5",
  "license": "csdn.net",
  "created_at": 1637894160,
  "topic_link": "https://bbs.csdn.net/topics/600470124"
}