{"id":1613,"date":"2018-01-16T18:41:12","date_gmt":"2018-01-17T02:41:12","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=1613"},"modified":"2018-09-03T10:17:59","modified_gmt":"2018-09-03T17:17:59","slug":"leetcode-69-sqrtx","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-69-sqrtx\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode. 69 Sqrt(x)"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 69 Sqrt(x) - \u5237\u9898\u627e\u5de5\u4f5c EP158\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/_K4f9I11hYI?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<p>\u9898\u76ee\u5927\u610f\uff1a\u8ba9\u4f60\u5b9e\u73b0\u5f00\u6839\u53f7\u51fd\u6570\uff0c\u53ea\u9700\u8981\u8fd4\u56de\u6574\u6570\u90e8\u5206\u3002<\/p>\n<p><strong>Problem:<\/strong><\/p>\n<p>Implement\u00a0<code>int sqrt(int x)<\/code>.<\/p>\n<p>Compute and return the square root of\u00a0<i>x<\/i>.<\/p>\n<p><b>x<\/b>\u00a0is guaranteed to be a non-negative integer.<\/p>\n<p><b>Example 1:<\/b><\/p>\n<pre class=\"\">Input: 4\r\nOutput: 2\r\n<\/pre>\n<p><b>Example 2:<\/b><\/p>\n<pre class=\"\">Input: 8\r\nOutput: 2\r\nExplanation: The square root of 8 is 2.82842...<\/pre>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1624\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/01\/69-ep158.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/01\/69-ep158.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/01\/69-ep158-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/01\/69-ep158-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1628\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/01\/69-ep158-2-1.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/01\/69-ep158-2-1.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/01\/69-ep158-2-1-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/01\/69-ep158-2-1-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/p>\n<p><ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-2404451723245401\" data-ad-slot=\"7983117522\">\u00a0<\/ins><\/p>\n<h1><strong>Solution 1: Brute force<\/strong><\/h1>\n<p>Time complexity: sqrt(x)<\/p>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 40 ms\r\nclass Solution {\r\npublic:\r\n  int mySqrt(int x) {\r\n    if (x &lt;= 1) return x;\r\n    for (long long s = 1; s &lt;= x; ++s)\r\n      if (s * s &gt; x) return s - 1;\r\n    return -1;\r\n  }\r\n};<\/pre>\n\n<\/div><h2 class=\"tabtitle\">C++ div<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:default decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 162 ms\r\nclass Solution {\r\npublic:\r\n  int mySqrt(int x) {\r\n    if (x &lt;= 1) return x;\r\n    for (int s = 1; s &lt;= x; ++s)\r\n      if (s &gt; x \/ s) return s - 1;\r\n    return -1;\r\n  }\r\n};<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Java<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:default decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 71 ms\r\nclass Solution {\r\n  public int mySqrt(int x) {\r\n    if (x &lt;= 1) return x;\r\n    for (long s = 1; s &lt;= x; ++s)\r\n      if (s * s &gt; x) return (int)s - 1;\r\n    return -1;\r\n  }\r\n}<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3 TLE<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true \">\"\"\"\r\nAuthor: Huahua\r\nTLE: 602 \/ 1017 test cases passed.\r\n\"\"\"\r\nclass Solution:\r\n  def mySqrt(self, x):\r\n    if x &lt;= 1: return x\r\n    s = 1\r\n    while True:\r\n      if s*s &gt; x: return s - 1\r\n      s += 1\r\n    return -1\r\n<\/pre>\n<\/div><\/div>\n<h1><strong>Solution 2: Binary search<\/strong><\/h1>\n<p>Time complexity: O(logn)<\/p>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 12 ms\r\nclass Solution {\r\npublic:\r\n  int mySqrt(int x) {      \r\n    long l = 1;\r\n    long r = static_cast&lt;long&gt;(x) + 1;\r\n    while (l &lt; r) {\r\n      long m = l + (r - l) \/ 2;\r\n      if (m * m &gt; x) { \r\n        r = m;\r\n      } else {\r\n        l = m + 1;\r\n      }\r\n    }\r\n    \/\/ l: smallest number such that l * l &gt; x\r\n    return l - 1;\r\n  }\r\n};<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Java<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:default decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 47 ms\r\nclass Solution {\r\n  public int mySqrt(int x) {      \r\n    int l = 1;\r\n    int r = x;\r\n    while (l &lt;= r) {\r\n      int m = l + (r - l) \/ 2;\r\n      if (m &gt; x \/ m) {\r\n        r = m - 1;\r\n      } else {\r\n        l = m + 1;\r\n      }\r\n    }\r\n    return r;\r\n  }\r\n}<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:python decode:true\">\"\"\"\r\nAuthor: Huahua\r\nRunning time: 119 ms\r\n\"\"\"\r\nclass Solution:\r\n  def mySqrt(self, a):\r\n    l = 1\r\n    r = a\r\n    while l &lt;= r:\r\n      m = l + (r - l) \/\/ 2\r\n      if m * m &gt; a:\r\n        r = m - 1\r\n      else:\r\n        l = m + 1\r\n    return r;\r\n<\/pre>\n<\/div><\/div>\n<h1><strong>Solution 3: Newton&#8217;s method<\/strong><\/h1>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++ \/ float<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:default decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 28 ms\r\nclass Solution {\r\npublic:\r\n    int mySqrt(int a) {\r\n      constexpr double epsilon = 1e-2;\r\n      double x = a;\r\n      while (x * x - a &gt; epsilon) {\r\n        x = (x + a \/ x) \/ 2.0;\r\n      }\r\n      return x;\r\n    }\r\n};<\/pre>\n\n<\/div><h2 class=\"tabtitle\">C++ \/ int<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 27 ms\r\nclass Solution {\r\npublic:\r\n  int mySqrt(int a) {\r\n    \/\/ f(x) = x^2 - a, find root of f(x)\r\n    \/\/ Newton's method\r\n    \/\/ f'(x) = 2x\r\n    \/\/ x' = x - f(x) \/ f'(x) = x - (1\/2*x - a\/(2*x))\r\n    \/\/    = (x + a \/ x) \/ 2\r\n    int x = a;\r\n    while (x &gt; a \/ x)\r\n      x = (x + a \/ x) \/ 2;\r\n    return x;\r\n  }\r\n};<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Java<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:default decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 44 ms\r\nclass Solution {\r\n  public int mySqrt(int a) {    \r\n    long x = a;\r\n    while (x * x &gt; a)\r\n      x = (x + a \/ x) \/ 2;    \r\n    return (int)x;\r\n  }\r\n}<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true \">\"\"\"\r\nAuthor: Huahua\r\nRunning time: 100 ms\r\n\"\"\"\r\nclass Solution:\r\n  def mySqrt(self, a):\r\n    x = a\r\n    while x * x &gt; a:\r\n      x = (x + a \/\/ x) \/\/ 2\r\n    return x\r\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u8ba9\u4f60\u5b9e\u73b0\u5f00\u6839\u53f7\u51fd\u6570\uff0c\u53ea\u9700\u8981\u8fd4\u56de\u6574\u6570\u90e8\u5206\u3002 Problem: Implement\u00a0int sqrt(int x). Compute and return the square root of\u00a0x. x\u00a0is guaranteed to be a non-negative integer. Example 1: Input: 4 Output: 2&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[149,163,49],"tags":[52,222,31,212,61],"class_list":["post-1613","post","type-post","status-publish","format-standard","hentry","category-binary-search","category-easy","category-math","tag-binary-search","tag-easy","tag-math","tag-newton","tag-sqrt","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1613","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=1613"}],"version-history":[{"count":17,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1613\/revisions"}],"predecessor-version":[{"id":3829,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1613\/revisions\/3829"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=1613"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=1613"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=1613"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}