{
  "type": "code_options",
  "author": "https://github.com/begeekmyfriend/leetcode",
  "source": "solution.md",
  "exercise_id": "06b1f69f839848c8ba12203ff5b602cc",
  "keywords": "位运算,数学,字符串,模拟",
  "title": "二进制求和",
  "desc": [
    {
      "content": "\n<p>给你两个二进制字符串，返回它们的和（用二进制表示）。</p><p>输入为 <strong>非空 </strong>字符串且只包含数字&nbsp;<code>1</code>&nbsp;和&nbsp;<code>0</code>。</p><p>&nbsp;</p><p><strong>示例&nbsp;1:</strong></p><pre><strong>输入:</strong> a = &quot;11&quot;, b = &quot;1&quot;<strong><br />输出:</strong> &quot;100&quot;</pre><p><strong>示例&nbsp;2:</strong></p><pre><strong>输入:</strong> a = &quot;1010&quot;, b = &quot;1011&quot;<strong><br />输出:</strong> &quot;10101&quot;</pre><p>&nbsp;</p><p><strong>提示：</strong></p><ul>\t<li>每个字符串仅由字符 <code>&#39;0&#39;</code> 或 <code>&#39;1&#39;</code> 组成。</li>\t<li><code>1 &lt;= a.length, b.length &lt;= 10^4</code></li>\t<li>字符串如果不是 <code>&quot;0&quot;</code> ，就都不含前导零。</li></ul>",
      "language": "markdown"
    }
  ],
  "answer": [
    {
      "content": "",
      "language": "cpp"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "cpp"
      }
    ],
    [
      {
        "content": "",
        "language": "cpp"
      }
    ],
    [
      {
        "content": "",
        "language": "cpp"
      }
    ]
  ],
  "template": {
    "content": "#include <bits/stdc++.h>\nusing namespace std;\nclass Solution\n{\npublic:\n\tstring addBinary(string a, string b)\n\t{\n\t\tstring res;\n\t\tint carry = 0;\n\t\tint i = a.length() - 1;\n\t\tint j = b.length() - 1;\n\t\tfor (; i >= 0 && j >= 0; i--, j--)\n\t\t{\n\t\t\tif (a[i] == '1' && b[j] == '1')\n\t\t\t{\n\t\t\t\tif (carry > 0)\n\t\t\t\t{\n\t\t\t\t\tres.push_back('1');\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tres.push_back('0');\n\t\t\t\t}\n\t\t\t\tcarry = 1;\n\t\t\t}\n\t\t\telse if (a[i] == '0' && b[j] == '0')\n\t\t\t{\n\t\t\t\tif (carry > 0)\n\t\t\t\t{\n\t\t\t\t\tres.push_back('1');\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tres.push_back('0');\n\t\t\t\t}\n\t\t\t\tcarry = 0;\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tif (carry > 0)\n\t\t\t\t{\n\t\t\t\t\tres.push_back('0');\n\t\t\t\t\tcarry = 1;\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tres.push_back('1');\n\t\t\t\t\tcarry = 0;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\twhile (i >= 0)\n\t\t{\n\t\t\tif (a[i--] == '1')\n\t\t\t{\n\t\t\t\tif (carry > 0)\n\t\t\t\t{\n\t\t\t\t\tres.push_back('0');\n\t\t\t\t\tcarry = 1;\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tres.push_back('1');\n\t\t\t\t\tcarry = 0;\n\t\t\t\t}\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tres.push_back(carry + '0');\n\t\t\t\tcarry = 0;\n\t\t\t}\n\t\t}\n\t\twhile (j >= 0)\n\t\t{\n\t\t\tif (b[j--] == '1')\n\t\t\t{\n\t\t\t\tif (carry > 0)\n\t\t\t\t{\n\t\t\t\t\tres.push_back('0');\n\t\t\t\t\tcarry = 1;\n\t\t\t\t}\n\t\t\t\telse\n\t\t\t\t{\n\t\t\t\t\tres.push_back('1');\n\t\t\t\t\tcarry = 0;\n\t\t\t\t}\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tres.push_back(carry + '0');\n\t\t\t\tcarry = 0;\n\t\t\t}\n\t\t}\n\t\tif (carry > 0)\n\t\t{\n\t\t\tres.push_back('1');\n\t\t}\n\t\treverse(res.begin(), res.end());\n\t\treturn res;\n\t}\n};",
    "language": "cpp"
  },
  "node_id": "dailycode-248bac1e9f8049a797197ed6b8d08f56",
  "license": "csdn.net",
  "created_at": 1637894160,
  "topic_link": "https://bbs.csdn.net/topics/600471009"
}