{"id":7773,"date":"2020-12-06T16:35:39","date_gmt":"2020-12-07T00:35:39","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7773"},"modified":"2020-12-06T16:36:17","modified_gmt":"2020-12-07T00:36:17","slug":"leetcode-1680-concatenation-of-consecutive-binary-numbers","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/bit\/leetcode-1680-concatenation-of-consecutive-binary-numbers\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1680. Concatenation of Consecutive Binary Numbers"},"content":{"rendered":"\n<p>Given an integer&nbsp;<code>n<\/code>, return&nbsp;<em>the&nbsp;<strong>decimal value<\/strong>&nbsp;of the binary string formed by concatenating the binary representations of&nbsp;<\/em><code>1<\/code><em>&nbsp;to&nbsp;<\/em><code>n<\/code><em>&nbsp;in order,&nbsp;<strong>modulo&nbsp;<\/strong><\/em><code>10<sup>9&nbsp;<\/sup>+ 7<\/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> 1\n<strong>Explanation: <\/strong>\"1\" in binary corresponds to the decimal value 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 = 3\n<strong>Output:<\/strong> 27\n<strong>Explanation: <\/strong>In binary, 1, 2, and 3 corresponds to \"1\", \"10\", and \"11\".\nAfter concatenating them, we have \"11011\", which corresponds to the decimal value 27.\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 = 12\n<strong>Output:<\/strong> 505379714\n<strong>Explanation<\/strong>: The concatenation results in \"1101110010111011110001001101010111100\".\nThe decimal value of that is 118505380540.\nAfter modulo 10<sup>9<\/sup> + 7, the result is 505379714.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= n &lt;= 10<sup>5<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Bit Operation<\/strong><\/h2>\n\n\n\n<p>f(n) = (f(n &#8211; 1) &lt;&lt; length(n)) | n<\/p>\n\n\n\n<p>length(n) increase by 1 whenever n is power of 2.<br>n = 1, YES, len = 1<br>n = 2, YES, len = 2<br>n = 3, NO, len = 2<br>n = 4, YES, len = 3<br>n = 5, NO, len = 3<br>n = 6, NO, len = 3<br>n = 7, NO, len = 3<br>n = 8, YES, len = 4<br>&#8230;<\/p>\n\n\n\n<p>Time complexity: O(n)<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 concatenatedBinary(int n) {\n    constexpr int kMod = 1e9 + 7;\n    long ans = 0;    \n    for (int i = 1, len = 0; i <= n; ++i) {\n      if ((i &#038; (i - 1)) == 0) ++len;\n      ans = ((ans << len) % kMod + i) % kMod;\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an integer&nbsp;n, return&nbsp;the&nbsp;decimal value&nbsp;of the binary string formed by concatenating the binary representations of&nbsp;1&nbsp;to&nbsp;n&nbsp;in order,&nbsp;modulo&nbsp;109&nbsp;+ 7. Example 1: Input: n = 1 Output: 1&#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":[],"class_list":["post-7773","post","type-post","status-publish","format-standard","hentry","category-bit","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7773","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=7773"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7773\/revisions"}],"predecessor-version":[{"id":7775,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7773\/revisions\/7775"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7773"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7773"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7773"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}