{"id":5034,"date":"2019-04-10T22:28:13","date_gmt":"2019-04-11T05:28:13","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5034"},"modified":"2019-04-10T22:30:34","modified_gmt":"2019-04-11T05:30:34","slug":"leetcode-67-add-binary","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-67-add-binary\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 67. Add Binary"},"content":{"rendered":"\n<p>Given two binary strings, return their sum (also a binary string).<\/p>\n\n\n\n<p>The input strings are both&nbsp;<strong>non-empty<\/strong>&nbsp;and contains only characters&nbsp;<code>1<\/code>&nbsp;or&nbsp;<code>0<\/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> a = \"11\", b = \"1\"\n<strong>Output:<\/strong> \"100\"<\/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> a = \"1010\", b = \"1011\"\n<strong>Output:<\/strong> \"10101\"<\/pre>\n\n\n\n<p>Solution: Big integer<\/p>\n\n\n\n<p>Similar to <a href=\"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-66-plus-one\/\">https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-66-plus-one\/<\/a><\/p>\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++\">\/\/ Author: Huahua\nclass Solution {\npublic:\n  string addBinary(string a, string b) {\n    int carry = 0;\n    int i = a.length() - 1;\n    int j = b.length() - 1;\n    string ans;\n    while (i >= 0 || j >= 0) {\n      int s = (i >= 0 ? a[i--] - '0' : 0) + \n              (j >= 0 ? b[j--] - '0' : 0) + \n              carry;\n      carry = s >> 1;\n      ans += '0' + (s & 1);\n    }\n    if (carry) ans += '1';\n    return {rbegin(ans), rend(ans)};\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given two binary strings, return their sum (also a binary string). The input strings are both&nbsp;non-empty&nbsp;and contains only characters&nbsp;1&nbsp;or&nbsp;0. Example 1: Input: a = &#8220;11&#8221;,&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[16,222,31],"class_list":["post-5034","post","type-post","status-publish","format-standard","hentry","category-math","tag-bit","tag-easy","tag-math","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5034","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=5034"}],"version-history":[{"count":6,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5034\/revisions"}],"predecessor-version":[{"id":5040,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5034\/revisions\/5040"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5034"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5034"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5034"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}