{"id":8808,"date":"2021-11-26T21:19:34","date_gmt":"2021-11-27T05:19:34","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8808"},"modified":"2021-11-26T21:25:51","modified_gmt":"2021-11-27T05:25:51","slug":"leetcode-69-sqrtx-2","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/binary-search\/leetcode-69-sqrtx-2\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 69. Sqrt(x)"},"content":{"rendered":"\n<p>Given a non-negative integer&nbsp;<code>x<\/code>,&nbsp;compute and return&nbsp;<em>the square root of<\/em>&nbsp;<code>x<\/code>.<\/p>\n\n\n\n<p>Since the return type&nbsp;is an integer, the decimal digits are&nbsp;<strong>truncated<\/strong>, and only&nbsp;<strong>the integer part<\/strong>&nbsp;of the result&nbsp;is returned.<\/p>\n\n\n\n<p><strong>Note:&nbsp;<\/strong>You are not allowed to use any built-in exponent function or operator, such as&nbsp;<code>pow(x, 0.5)<\/code>&nbsp;or&nbsp;<code>x ** 0.5<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> x = 4\n<strong>Output:<\/strong> 2\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> x = 8\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> The square root of 8 is 2.82842..., and since the decimal part is truncated, 2 is returned.<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>0 &lt;= x &lt;= 2<sup>31<\/sup>&nbsp;- 1<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1: Binary Search<\/strong><\/h2>\n\n\n\n<p>Find the smallest l such that l * l > x, sqrt(x) = l &#8211; 1.<\/p>\n\n\n\n<p>Time complexity: O(logx)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int mySqrt(int x) {      \n    unsigned l = 1;\n    unsigned r = 1u + x;\n    while (l < r) {\n      unsigned m = l + (r - l) \/ 2;\n      if (m > x \/ m) { \n        r = m;\n      } else {\n        l = m + 1;\n      }\n    }\n    return l - 1;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given a non-negative integer&nbsp;x,&nbsp;compute and return&nbsp;the square root of&nbsp;x. Since the return type&nbsp;is an integer, the decimal digits are&nbsp;truncated, and only&nbsp;the integer part&nbsp;of the result&nbsp;is&#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],"tags":[52,31,177],"class_list":["post-8808","post","type-post","status-publish","format-standard","hentry","category-binary-search","tag-binary-search","tag-math","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8808","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=8808"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8808\/revisions"}],"predecessor-version":[{"id":8810,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8808\/revisions\/8810"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8808"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8808"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8808"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}