{
  "type": "code_options",
  "author": "csdn.net",
  "source": "solution.md",
  "exercise_id": "2627bbe764734181a8fea0dfd6b97207",
  "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": "java"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "java"
      }
    ],
    [
      {
        "content": "",
        "language": "java"
      }
    ],
    [
      {
        "content": "",
        "language": "java"
      }
    ]
  ],
  "template": {
    "content": "class Solution {\n\tpublic String addBinary(String a, String b) {\n\n\t\tStringBuffer s1 = new StringBuffer(a);\n\t\ts1.reverse();\n\t\tStringBuffer s2 = new StringBuffer(b);\n\t\ts2.reverse();\n\n\t\tif (s1.length() > s2.length()) {\n\t\t\tint n = s1.length() - s2.length();\n\t\t\tfor (int i = 0; i < n; i++) {\n\t\t\t\ts2.append('0');\n\t\t\t}\n\t\t} else if (s1.length() < s2.length()) {\n\t\t\tint n = s2.length() - s1.length();\n\t\t\tfor (int i = 0; i < n; i++) {\n\t\t\t\ts1.append('0');\n\t\t\t}\n\t\t}\n\n\t\tStringBuffer stringBuffer = new StringBuffer(\"\");\n\t\tint i = 0;\n\t\tchar flag = '0';\n\t\twhile (i < s1.length() && i < s2.length()) {\n\t\t\tif (flag == '0') {\n\t\t\t\tif (s1.charAt(i) == s2.charAt(i) && s1.charAt(i) == '1') {\n\t\t\t\t\tflag = '1';\n\t\t\t\t\tstringBuffer.append('0');\n\t\t\t\t} else if (s1.charAt(i) == s2.charAt(i) && s1.charAt(i) == '0') {\n\t\t\t\t\tstringBuffer.append('0');\n\t\t\t\t} else {\n\t\t\t\t\tstringBuffer.append('1');\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tif (s1.charAt(i) == s2.charAt(i) && s1.charAt(i) == '1') {\n\t\t\t\t\tflag = '1';\n\t\t\t\t\tstringBuffer.append('1');\n\t\t\t\t} else if (s1.charAt(i) == s2.charAt(i) && s1.charAt(i) == '0') {\n\t\t\t\t\tflag = '0';\n\t\t\t\t\tstringBuffer.append('1');\n\t\t\t\t} else {\n\t\t\t\t\tflag = '1';\n\t\t\t\t\tstringBuffer.append('0');\n\t\t\t\t}\n\t\t\t}\n\t\t\ti++;\n\t\t}\n\t\tif (flag == '1') {\n\t\t\tstringBuffer.append(flag);\n\t\t}\n\t\tstringBuffer.reverse();\n\t\treturn stringBuffer.toString();\n\t}\n}",
    "language": "java"
  },
  "node_id": "dailycode-8fcebe8098d040f7a3dbbb9a40d79501",
  "license": "csdn.net",
  "created_at": 1637894161,
  "topic_link": "https://bbs.csdn.net/topics/600471009"
}