{"id":1363,"date":"2017-12-29T22:17:27","date_gmt":"2017-12-30T06:17:27","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=1363"},"modified":"2018-01-01T15:53:10","modified_gmt":"2018-01-01T23:53:10","slug":"leetcode-367-valid-perfect-square","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-367-valid-perfect-square\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 367. Valid Perfect Square"},"content":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u5224\u65ad\u4e00\u4e2a\u6570\u662f\u5426\u662f\u5e73\u65b9\u6570\u3002\u4e0d\u80fd\u4f7f\u7528\u5f00\u6839\u53f7\u51fd\u6570\u3002<\/p>\n<p>Given a positive integer\u00a0<i>num<\/i>, write a function which returns True if\u00a0<i>num<\/i>\u00a0is a perfect square else False.<\/p>\n<p><b>Note:<\/b>\u00a0<b>Do not<\/b>\u00a0use any built-in library function such as\u00a0<code>sqrt<\/code>.<\/p>\n<p><b>Example 1:<\/b><\/p>\n<pre class=\"\">Input: 16\r\nReturns: True\r\n<\/pre>\n<p><b>Example 2:<\/b><\/p>\n<pre class=\"\">Input: 14\r\nReturns: False<\/pre>\n<p><strong>Idea:<\/strong><\/p>\n<p>Binary search<\/p>\n<p><strong>Solution:<\/strong><\/p>\n<p>C++<\/p>\n<p>Time complexity: O(log(num))<\/p>\n<p>Space complexity: O(1)<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Runtime: 0 ms\r\nclass Solution {\r\npublic:\r\n    bool isPerfectSquare(int num) {        \r\n        long l = 1;\r\n        long r = num + 1;\r\n        while (l &lt; r) {\r\n            long m = l + (r - l) \/ 2;\r\n            long t = m * m;\r\n            if (t == num)\r\n                return true;\r\n            else if (t &gt; num)\r\n                r = m;\r\n            else\r\n                l = m + 1;\r\n        }\r\n        \r\n        return false;\r\n    }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u5224\u65ad\u4e00\u4e2a\u6570\u662f\u5426\u662f\u5e73\u65b9\u6570\u3002\u4e0d\u80fd\u4f7f\u7528\u5f00\u6839\u53f7\u51fd\u6570\u3002 Given a positive integer\u00a0num, write a function which returns True if\u00a0num\u00a0is a perfect square else False. Note:\u00a0Do not\u00a0use any built-in library function such as\u00a0sqrt.&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[163,49],"tags":[],"class_list":["post-1363","post","type-post","status-publish","format-standard","hentry","category-easy","category-math","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1363","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=1363"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1363\/revisions"}],"predecessor-version":[{"id":1435,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1363\/revisions\/1435"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=1363"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=1363"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=1363"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}