{
  "type": "code_options",
  "author": "https://github.com/begeekmyfriend/leetcode",
  "source": "solution.md",
  "exercise_id": "3b5534cc405947d595b17441f3433545",
  "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": "cpp"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "cpp"
      }
    ],
    [
      {
        "content": "",
        "language": "cpp"
      }
    ],
    [
      {
        "content": "",
        "language": "cpp"
      }
    ]
  ],
  "template": {
    "content": "#include <bits/stdc++.h>\nusing namespace std;\nclass Solution\n{\npublic:\n\tint mySqrt(int x)\n\t{\n\t\tif (x == 0)\n\t\t{\n\t\t\treturn 0;\n\t\t}\n\t\tunsigned int lo = 1, hi = x;\n\t\tunsigned int mid = (lo + hi) / 2;\n\t\tfor (;;)\n\t\t{\n\t\t\tif (mid > x / mid)\n\t\t\t{\n\t\t\t\thi = mid;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tif (mid + 1 > x / (mid + 1))\n\t\t\t\t{\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tlo = mid;\n\t\t\t\t}\n\t\t\t}\n\t\t\tmid = (lo + hi) / 2;\n\t\t}\n\t\treturn mid;\n\t}\n};",
    "language": "cpp"
  },
  "node_id": "dailycode-14c9f2ce4a074e43958cde1273f03140",
  "license": "csdn.net",
  "created_at": 1637894160,
  "topic_link": "https://bbs.csdn.net/topics/600470124"
}