# 二进制求和

<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>	<li>每个字符串仅由字符 <code>&#39;0&#39;</code> 或 <code>&#39;1&#39;</code> 组成。</li>	<li><code>1 &lt;= a.length, b.length &lt;= 10^4</code></li>	<li>字符串如果不是 <code>&quot;0&quot;</code> ，就都不含前导零。</li></ul>

## template

```cpp
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
	string addBinary(string a, string b)
	{
		string res;
		int carry = 0;
		int i = a.length() - 1;
		int j = b.length() - 1;
		for (; i >= 0 && j >= 0; i--, j--)
		{
			if (a[i] == '1' && b[j] == '1')
			{
				if (carry > 0)
				{
					res.push_back('1');
				}
				else
				{
					res.push_back('0');
				}
				carry = 1;
			}
			else if (a[i] == '0' && b[j] == '0')
			{
				if (carry > 0)
				{
					res.push_back('1');
				}
				else
				{
					res.push_back('0');
				}
				carry = 0;
			}
			else
			{
				if (carry > 0)
				{
					res.push_back('0');
					carry = 1;
				}
				else
				{
					res.push_back('1');
					carry = 0;
				}
			}
		}
		while (i >= 0)
		{
			if (a[i--] == '1')
			{
				if (carry > 0)
				{
					res.push_back('0');
					carry = 1;
				}
				else
				{
					res.push_back('1');
					carry = 0;
				}
			}
			else
			{
				res.push_back(carry + '0');
				carry = 0;
			}
		}
		while (j >= 0)
		{
			if (b[j--] == '1')
			{
				if (carry > 0)
				{
					res.push_back('0');
					carry = 1;
				}
				else
				{
					res.push_back('1');
					carry = 0;
				}
			}
			else
			{
				res.push_back(carry + '0');
				carry = 0;
			}
		}
		if (carry > 0)
		{
			res.push_back('1');
		}
		reverse(res.begin(), res.end());
		return res;
	}
};
```

## 答案

```cpp

```

## 选项

### A

```cpp

```

### B

```cpp

```

### C

```cpp

```