> For the complete documentation index, see [llms.txt](https://dfine.gitbook.io/leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://dfine.gitbook.io/leetcode/67.add_binary.md).

# 67.Add Binary

**67.Add Binary**

难度:Easy

> 给定两个二进制字符串，返回他们的和（用二进制表示）。

输入为非空字符串且只包含数字 1 和 0。

示例 1:

输入: a = "11", b = "1" 输出: "100" 示例 2:

输入: a = "1010", b = "1011" 输出: "10101"

按普通加法一位一位算，留一个进位标志。

```
class Solution {
public:
    string addBinary(string a, string b) {
        a='0'+a;
        b='0'+b;
     //   cout<<a<< " " <<b <<endl;
        if(a.length()>b.length())
        {
            int tmp=a.length()-b.length();
            while(tmp--)
            b= '0'+b;
            
        }
        else if(a.length()<b.length())
        {
            int tmp=b.length()-a.length();
            while(tmp--)
            a='0' +a;
        }
    //     cout<<a<< " " <<b <<endl;
        bool flag=false;
        int len=a.length();
        string res;
        for(int i=len-1;i>0;i--)
        {
            if(a[i] ^ b[i])
            {
                if(flag)
                    res='0'+res;
                else
                    res = '1'+res;
            }
            else
            {
                if(a[i]=='0')
                {
                    if(flag)
                    {
                        res= '1'+res;
                        flag=false;
                    }
                    else 
                        res= '0'+res;
                }
                else
                {
                    if(flag)
                        res= '1'+res;
                    else
                    {
                        res='0'+res;
                        flag=true;
                    }
                }
            }
        }
        if(flag) res='1'+res;
        return res;
    }
};
```

> 执行用时 :16 ms, 在所有 C++ 提交中击败了24.74%的用户\
> 内存消耗 :9.3 MB, 在所有 C++ 提交中击败了6.03%的用户


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://dfine.gitbook.io/leetcode/67.add_binary.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
