{"id":5646,"date":"2019-09-30T01:32:29","date_gmt":"2019-09-30T08:32:29","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5646"},"modified":"2019-12-14T10:45:30","modified_gmt":"2019-12-14T18:45:30","slug":"leetcode-190-reverse-bits","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/bit\/leetcode-190-reverse-bits\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 190. Reverse Bits"},"content":{"rendered":"\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 190. Reverse Bits - \u5237\u9898\u627e\u5de5\u4f5c EP284\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/K0EHvvbUdEg?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>\n<\/div><\/figure>\n\n\n\n<p>Reverse bits of a given 32 bits unsigned integer.<\/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> 00000010100101000001111010011100\n<strong>Output:<\/strong> 00111001011110000010100101000000\n<strong>Explanation: <\/strong>The input binary string <strong>00000010100101000001111010011100<\/strong> represents the unsigned integer 43261596, so return 964176192 which its binary representation is <strong>00111001011110000010100101000000<\/strong>.\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> 11111111111111111111111111111101\n<strong>Output:<\/strong> 10111111111111111111111111111111\n<strong>Explanation: <\/strong>The input binary string <strong>11111111111111111111111111111101<\/strong> represents the unsigned integer 4294967293, so return 3221225471 which its binary representation is <strong>10101111110010110010011101101001<\/strong>.<\/pre>\n\n\n\n<p><strong>Note:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Note that in some languages such as Java, there is no unsigned integer type. In this case, both input and output will be given as signed integer type and should not affect your implementation, as the internal binary representation of the integer is the same whether it is signed or unsigned.<\/li><li>In Java,&nbsp;the compiler represents the signed integers using&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Two%27s_complement\" target=\"_blank\" rel=\"noreferrer noopener\">2&#8217;s complement notation<\/a>. Therefore, in&nbsp;<strong>Example 2<\/strong>&nbsp;above the input represents the signed integer&nbsp;<code>-3<\/code>&nbsp;and the output represents the signed integer&nbsp;<code>-1073741825<\/code>.<\/li><\/ul>\n\n\n\n<p><strong>Follow up<\/strong>:<\/p>\n\n\n\n<p>If this function is called many times, how would you optimize it?<\/p>\n\n\n\n<p>Solution: Bit operation<\/p>\n\n\n\n<p>Time complexity: O(1)<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  uint32_t reverseBits(uint32_t n) {\n    uint32_t ans = 0;\n    for (int i = 0; i < 32; ++i) {\n      ans <<= 1;\n      ans |= n &#038; 1;      \n      n >>= 1;\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Reverse bits of a given 32 bits unsigned integer. Example 1: Input: 00000010100101000001111010011100 Output: 00111001011110000010100101000000 Explanation: The input binary string 00000010100101000001111010011100 represents the unsigned integer&#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,92,246],"class_list":["post-5646","post","type-post","status-publish","format-standard","hentry","category-bit","tag-bit","tag-easy","tag-o1","tag-reverse","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5646","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=5646"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5646\/revisions"}],"predecessor-version":[{"id":5957,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5646\/revisions\/5957"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5646"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5646"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5646"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}