# 方法一

class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
*
*
* @param A string字符串
* @param B string字符串
* @return string字符串
*/
string binaryAdd(string A, string B) {
// write code here
vector<int> a,b,c;
string s;
for (int i=A.size()-1;i>=0;i--)
{
a.push_back(A[i]-'0');
}
for (int i=B.size()-1;i>=0;i--)
{
b.push_back(B[i]-'0');
}
int i = 0, jinwei = 0;
while (i<A.size() && i<B.size())
{
//计算结果的每一位和进位
c.push_back((a[i]+b[i]+jinwei)%2);
jinwei = (a[i]+b[i]+jinwei)/2;
i++;
}
//处理剩余的数字
if (i==A.size())
{
while (i<B.size())
{
c.push_back((b[i]+jinwei)%2);
jinwei = (b[i]+jinwei)/2;
i++;
}
}
else
{
while (i<A.size())
{
c.push_back((a[i]+jinwei)%2);
jinwei = (a[i]+jinwei)/2;
i++;
}
}
if (jinwei) c.push_back(1);
for (int j=c.size()-1;j>=0;j--)
{
s = s + to_string(c[j]);
}
return s;
}

};


# 方法二

class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
*
*
* @param A string字符串
* @param B string字符串
* @return string字符串
*/
string binaryAdd(string A, string B) {
// write code here
string s;
int jinwei = 0, i;
int n = A.size()-1, m = B.size()-1;
for (i=0;i<=n && i<=m;i++)
{
//使用异或进行计算
s = s + to_string(A[n-i]^B[m-i]^jinwei);
if ((A[n-i]==B[m-i] && A[n-i]=='1') || (A[n-i]!=B[m-i] && jinwei))
jinwei = 1;
else jinwei = 0;
}
//处理剩余的位数
if (i==(n+1))
{
while (i<=m)
{
s = s + to_string((B[m-i]-'0')^jinwei);
jinwei = jinwei && (B[m-i]=='1');
i++;
}
}
else
{
while (i<=n)
{
s = s + to_string((A[n-i]-'0')^jinwei);
jinwei = jinwei && (A[n-i]=='1');
i++;
}
}
if (jinwei) s = s + '1';
reverse(s.begin(), s.end());
return s;
}

};