solution.md 4.4 KB
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1
# 二进制求和
F
fix bug  
feilong 已提交
2

每日一练社区's avatar
每日一练社区 已提交
3
<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>
每日一练社区's avatar
每日一练社区 已提交
4
<p>以下<span style="color:red">错误</span>的选项是?</p>
F
fix bug  
feilong 已提交
5

每日一练社区's avatar
每日一练社区 已提交
6
## aop
F
fix bug  
feilong 已提交
7

每日一练社区's avatar
每日一练社区 已提交
8
### before
F
fix bug  
feilong 已提交
9

每日一练社区's avatar
每日一练社区 已提交
10 11 12 13 14
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
F
fix bug  
feilong 已提交
15

每日一练社区's avatar
每日一练社区 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28 29
```cpp
int main()
{
    Solution sol;
    string a = "1010";
    string b = "1011";
    string res;
    res = sol.addBinary(a, b);
    cout << res;
    return 0;
}
```

## 答案
F
fix bug  
feilong 已提交
30

每日一练社区's avatar
每日一练社区 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
```cpp
class Solution
{
public:
    string addBinary(string a, string b)
    {
        string res;
        int carry = 0;
        int m = a.size() - 1, n = b.size() - 1;
        while (m >= 0 || n >= 0)
        {
            int q = (m >= 0) ? a[m] - '0' : 0;
            int p = (n >= 0) ? b[n] - '0' : 0;
            int k = q + p + carry;
            res = to_string(k % 2) + res;
            carry = k / 2;
        }
        if (1 == carry)
            res = '1' + res;
        return res;
    }
};
```
## 选项

F
fix bug  
feilong 已提交
56

每日一练社区's avatar
每日一练社区 已提交
57
### A
F
fix bug  
feilong 已提交
58

每日一练社区's avatar
每日一练社区 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
```cpp
class Solution
{
public:
    string addBinary(string a, string b)
    {
        long aa = 0, bb = 0;
        long sum = 0;
        string res;
        int p = 0;
        stringstream outa(a);
        stringstream outb(b);
        outa >> aa;
        outb >> bb;
        sum = aa + bb;
        bool flag = false;
        while (sum != 0)
        {
            p = sum % 10;
            if (flag == true)
                p++;
            flag = false;
            sum /= 10;
            if (p == 0)
                res = res + "0";
            else if (p % 2 == 0)
            {
                flag = true;
                res = res + "0";
            }
            else
                res = res + "1";
        }
        if (res[res.size() - 1] == '0')
            res = res + "1";
        reverse(res.begin(), res.end());
        return res;
    }
};
```

### B
F
fix bug  
feilong 已提交
101

每日一练社区's avatar
每日一练社区 已提交
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
```cpp
class Solution
{
public:
    string addBinary(string a, string b)
    {
        if (b.size() > a.size())
        {
            string temp = b;
            b = a;
            a = temp;
        }
        int i = a.size() - 1;
        int j = b.size() - 1;
        if (i != j)
        {
            for (int k = 0; k < i - j; k++)
                b = "0" + b;
        }
        int count = 0;
        for (int k = i; k >= 0; k--)
        {
            if (a[k] - '0' + b[k] - '0' + count == 0)
            {
                a[k] = '0';
                count = 0;
            }
            else if (a[k] - '0' + b[k] - '0' + count == 1)
            {
                a[k] = '1';
                count = 0;
            }
            else if (a[k] - '0' + b[k] - '0' + count == 3)
            {
                a[k] = '1';
                count = 1;
            }
            else
            {
                a[k] = '0';
                count = 1;
            }
        }
        if (count == 1)
            a = '1' + a;
        return a;
    }
};
```

### C
F
fix bug  
feilong 已提交
153

每日一练社区's avatar
每日一练社区 已提交
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
```cpp
class Solution
{
public:
    string addBinary(string a, string b)
    {
        string result = "", rr = "";
        char aa, bb;
        int l1 = a.length(), l2 = b.length(), i = l1 - 1, j = l2 - 1, carry = 0, sum = 0;
        while (true)
        {
            if (i < 0)
                aa = '0';
            else
                aa = a[i];
            if (j < 0)
                bb = '0';
            else
                bb = b[j];
            sum = (aa - '0') + (bb - '0') + carry;
            result += ((sum % 2) + '0');
            carry = sum / 2;
            j--;
            i--;
            if (i < 0 && j < 0)
            {
                if (carry == 1)
                    result += "1";
                break;
            }
        }
        int l3 = result.length();
        for (int i = l3 - 1; i >= 0; i--)
            rr += result[i];
        return rr;
    }
};
```