# 二进制求和

<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

```java
class Solution {
	public String addBinary(String a, String b) {

		StringBuffer s1 = new StringBuffer(a);
		s1.reverse();
		StringBuffer s2 = new StringBuffer(b);
		s2.reverse();

		if (s1.length() > s2.length()) {
			int n = s1.length() - s2.length();
			for (int i = 0; i < n; i++) {
				s2.append('0');
			}
		} else if (s1.length() < s2.length()) {
			int n = s2.length() - s1.length();
			for (int i = 0; i < n; i++) {
				s1.append('0');
			}
		}

		StringBuffer stringBuffer = new StringBuffer("");
		int i = 0;
		char flag = '0';
		while (i < s1.length() && i < s2.length()) {
			if (flag == '0') {
				if (s1.charAt(i) == s2.charAt(i) && s1.charAt(i) == '1') {
					flag = '1';
					stringBuffer.append('0');
				} else if (s1.charAt(i) == s2.charAt(i) && s1.charAt(i) == '0') {
					stringBuffer.append('0');
				} else {
					stringBuffer.append('1');
				}
			} else {
				if (s1.charAt(i) == s2.charAt(i) && s1.charAt(i) == '1') {
					flag = '1';
					stringBuffer.append('1');
				} else if (s1.charAt(i) == s2.charAt(i) && s1.charAt(i) == '0') {
					flag = '0';
					stringBuffer.append('1');
				} else {
					flag = '1';
					stringBuffer.append('0');
				}
			}
			i++;
		}
		if (flag == '1') {
			stringBuffer.append(flag);
		}
		stringBuffer.reverse();
		return stringBuffer.toString();
	}
}
```

## 答案

```java

```

## 选项

### A

```java

```

### B

```java

```

### C

```java

```