{"id":9069,"date":"2021-12-07T21:22:11","date_gmt":"2021-12-08T05:22:11","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9069"},"modified":"2021-12-07T21:22:44","modified_gmt":"2021-12-08T05:22:44","slug":"leetcode-231-power-of-two","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/bit\/leetcode-231-power-of-two\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 231. Power of Two"},"content":{"rendered":"\n<p>Given an integer&nbsp;<code>n<\/code>, return&nbsp;<em><code>true<\/code>&nbsp;if it is a power of two. Otherwise, return&nbsp;<code>false<\/code><\/em>.<\/p>\n\n\n\n<p>An integer&nbsp;<code>n<\/code>&nbsp;is a power of two, if there exists an integer&nbsp;<code>x<\/code>&nbsp;such that&nbsp;<code>n == 2<sup>x<\/sup><\/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> n = 1\n<strong>Output:<\/strong> true\n<strong>Explanation: <\/strong>2<sup>0<\/sup> = 1\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> n = 16\n<strong>Output:<\/strong> true\n<strong>Explanation: <\/strong>2<sup>4<\/sup> = 16\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 3\n<strong>Output:<\/strong> false\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 4\n<strong>Output:<\/strong> true\n<\/pre>\n\n\n\n<p><strong>Example 5:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 5\n<strong>Output:<\/strong> false\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>-2<sup>31<\/sup>&nbsp;&lt;= n &lt;= 2<sup>31<\/sup>&nbsp;- 1<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: 1 bit set<\/strong><\/h2>\n\n\n\n<p>Any power of two has only 1 bit set in the binary format. e.g. 1=0b01, 2=0b10, 4=0b100, 8=0b1000<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>use popcount.<\/li><\/ol>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\/\/ Author: Huahua\nclass Solution {\npublic:\n  bool isPowerOfTwo(int n) {\n     return n > 0 ? __builtin_popcount(n) == 1 : false;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p>2. Use (n) &amp; (n &#8211; 1) to remove the last set bit, so it should be zero.<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\/\/ Author: Huahua\nclass Solution {\npublic:\n  bool isPowerOfTwo(int n) {\n     return n > 0 ? !(n & (n - 1)) : false;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given an integer&nbsp;n, return&nbsp;true&nbsp;if it is a power of two. Otherwise, return&nbsp;false. An integer&nbsp;n&nbsp;is a power of two, if there exists an integer&nbsp;x&nbsp;such that&nbsp;n ==&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[126],"tags":[16,222],"class_list":["post-9069","post","type-post","status-publish","format-standard","hentry","category-bit","tag-bit","tag-easy","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9069","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=9069"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9069\/revisions"}],"predecessor-version":[{"id":9071,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9069\/revisions\/9071"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9069"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9069"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9069"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}