Add to List 67. Add Binary(LeetCode)

Given two binary strings, return their sum (also a binary string).

For example,
a = "11"
b = "1"
Return "100".

 1 class Solution {
 2 public:
 3     string addBinary(string a, string b) {
 4         string str = "";
 5         int alen = a.size();
 6         int blen = b.size();
 7         if (alen == 0)
 8             return b;
 9         if (blen == 0)
10             return a;
11         int i = alen-1;
12         int j = blen-1;
13         int flag = 0;
14         while (i>=0&&j>=0)
15         {
16             if ((a[i] - '0' + b[j] - '0') + flag <= 1)
17             {
18                 str += ((a[i] - '0' + b[j] - '0') + flag + '0');
19                 flag = 0;
20                 i--;
21                 j--;
22             }
23             else
24             {
25                 if ((a[i] - '0' + b[j] - '0') + flag == 2)
26                 {
27                     str += "0";
28                     if (i == 0 && j == 0)
29                         str += "1";
30                     else
31                     flag = 1;
32                 }
33                 else
34                 {
35                     str += "1";
36                     if (i == 0 && j == 0)
37                         str += "1";
38                     else
39                     flag = 1;
40                 }
41                 i--; j--;
42             }
43         }
44         if (i < 0)
45         {
46             while (j >= 0)
47             {
48                 if (b[j] - '0' + flag <= 1)
49                 {
50                     str += (b[j] - '0' + flag) + '0';
51                     flag=0;
52                 }
53                 else
54                 {
55                     str += "0";
56                     if (j == 0)
57                         str += "1";
58                     else
59                     flag = 1;
60                 }
61                 j--;
62             }
63         }
64         else
65         {
66             while (i >= 0)
67             {
68                 if (a[i] - '0' + flag <= 1)
69                 {
70                     str += (a[i] - '0' + flag) + '0';
71                     flag=0;
72                 }
73                 else
74                 {
75                     str += "0";
76                     if (i == 0)
77                         str += "1";
78                     else
79                     flag = 1;
80                 }
81                 i--;
82             }
83         }
84         string str1 = "";
85         for (int i = str.size()-1; i >= 0; --i)
86         {
87             str1 += str[i];
88         }
89         return str1;
90     }
91 };
 1 class Solution {
 2 public:
 3     string addBinary(string a, string b) {
 4         int ai=a.size()-1,bi=b.size()-1,extra=0,s,r;
 5         string ans;
 6         while(ai>=0&&bi>=0){
 7             s=a[ai]-'0'+b[bi]-'0'+extra;
 8             r=s%2;
 9             extra=s/2;
10             ans.push_back(r+'0');
11             ai--;
12             bi--;
13         }
14         while(ai>=0){
15             s=a[ai]-'0'+extra;
16             r=s%2;
17             extra=s/2;
18             ans.push_back(r+'0');
19             ai--;
20         }
21         while(bi>=0){
22             s=b[bi]-'0'+extra;
23             r=s%2;
24             extra=s/2;
25             ans.push_back(r+'0');
26             bi--;
27         }
28         if(extra) ans.push_back('1');
29         reverse(ans.begin(),ans.end());
30         return ans;
31     }
32 };
原文地址:https://www.cnblogs.com/wujufengyun/p/7130752.html