{"id":5225,"date":"2019-06-06T22:10:38","date_gmt":"2019-06-07T05:10:38","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5225"},"modified":"2019-06-06T22:11:04","modified_gmt":"2019-06-07T05:11:04","slug":"leetcode-1073-adding-two-negabinary-numbers","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/leetcode-1073-adding-two-negabinary-numbers\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1073. Adding Two Negabinary Numbers"},"content":{"rendered":"\n<p>Given two numbers&nbsp;<code>arr1<\/code>&nbsp;and&nbsp;<code>arr2<\/code>&nbsp;in base&nbsp;<strong>-2<\/strong>, return the result of adding them together.<\/p>\n\n\n\n<p>Each number is given in&nbsp;<em>array format<\/em>:&nbsp; as an array of 0s and 1s, from most significant bit to least significant bit.&nbsp; For example,&nbsp;<code>arr = [1,1,0,1]<\/code>&nbsp;represents the number&nbsp;<code>(-2)^3&nbsp;+ (-2)^2 + (-2)^0 = -3<\/code>.&nbsp; A number&nbsp;<code>arr<\/code>&nbsp;in&nbsp;<em>array format<\/em>&nbsp;is also guaranteed to have no leading zeros: either&nbsp;<code>arr == [0]<\/code>&nbsp;or&nbsp;<code>arr[0] == 1<\/code>.<\/p>\n\n\n\n<p>Return the result of adding&nbsp;<code>arr1<\/code>&nbsp;and&nbsp;<code>arr2<\/code>&nbsp;in the same format: as an array of 0s and 1s with no leading zeros.<\/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>arr1 = [1,1,1,1,1], arr2 = [1,0,1]\n<strong>Output: <\/strong>[1,0,0,0,0]\n<strong>Explanation: <\/strong>arr1 represents 11, arr2 represents 5, the output represents 16.\n<\/pre>\n\n\n\n<p><strong>Note:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><code>1 &lt;= arr1.length &lt;= 1000<\/code><\/li><li><code>1 &lt;= arr2.length &lt;= 1000<\/code><\/li><li><code>arr1<\/code>&nbsp;and&nbsp;<code>arr2<\/code>&nbsp;have no leading zeros<\/li><li><code>arr1[i]<\/code>&nbsp;is&nbsp;<code>0<\/code>&nbsp;or&nbsp;<code>1<\/code><\/li><li><code>arr2[i]<\/code>&nbsp;is&nbsp;<code>0<\/code>&nbsp;or&nbsp;<code>1<\/code><\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Simulation<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/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, 8 ms, 9.2 MB\nclass Solution {\npublic:\n  vector<int> addNegabinary(vector<int>& arr1, vector<int>& arr2) {\n    int i = arr1.size();\n    int j = arr2.size();\n    int carry = 0;\n    vector<int> ans;\n    while (i || j || carry) {\n      int sum =  (i ? arr1[--i] : 0) + (j ? arr2[--j] : 0) + carry;      \n      ans.push_back(sum & 1);\n      carry = -(sum >> 1);\n    }\n    while (ans.back() == 0 && ans.size() > 1) ans.pop_back();\n    reverse(begin(ans), end(ans));\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given two numbers&nbsp;arr1&nbsp;and&nbsp;arr2&nbsp;in base&nbsp;-2, return the result of adding them together. Each number is given in&nbsp;array format:&nbsp; as an array of 0s and 1s, from&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48],"tags":[16,179],"class_list":["post-5225","post","type-post","status-publish","format-standard","hentry","category-simulation","tag-bit","tag-simulation","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5225","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=5225"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5225\/revisions"}],"predecessor-version":[{"id":5227,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5225\/revisions\/5227"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5225"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5225"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5225"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}