@@ -114,6 +114,39 @@ class Solution {
114114}
115115```
116116
117+ ``` csharp
118+ public class Solution {
119+ public string AddBinary (string a , string b ) {
120+ StringBuilder res = new StringBuilder ();
121+ int carry = 0 ;
122+
123+ char [] sa = a .ToCharArray ();
124+ char [] sb = b .ToCharArray ();
125+ Array .Reverse (sa );
126+ Array .Reverse (sb );
127+
128+ int n = Math .Max (sa .Length , sb .Length );
129+
130+ for (int i = 0 ; i < n ; i ++ ) {
131+ int digitA = i < sa .Length ? sa [i ] - '0' : 0 ;
132+ int digitB = i < sb .Length ? sb [i ] - '0' : 0 ;
133+
134+ int total = digitA + digitB + carry ;
135+ res .Append ((char )((total % 2 ) + '0' ));
136+ carry = total / 2 ;
137+ }
138+
139+ if (carry > 0 ) {
140+ res .Append ('1' );
141+ }
142+
143+ char [] resultArray = res .ToString ().ToCharArray ();
144+ Array .Reverse (resultArray );
145+ return new string (resultArray );
146+ }
147+ }
148+ ```
149+
117150:: tabs-end
118151
119152### Time & Space Complexity
@@ -230,6 +263,32 @@ class Solution {
230263}
231264```
232265
266+ ``` csharp
267+ public class Solution {
268+ public string AddBinary (string a , string b ) {
269+ StringBuilder res = new StringBuilder ();
270+ int carry = 0 ;
271+
272+ int i = a .Length - 1 , j = b .Length - 1 ;
273+ while (i >= 0 || j >= 0 || carry > 0 ) {
274+ int digitA = i >= 0 ? a [i ] - '0' : 0 ;
275+ int digitB = j >= 0 ? b [j ] - '0' : 0 ;
276+
277+ int total = digitA + digitB + carry ;
278+ res .Append (total % 2 );
279+ carry = total / 2 ;
280+
281+ i -- ;
282+ j -- ;
283+ }
284+
285+ char [] resultArray = res .ToString ().ToCharArray ();
286+ Array .Reverse (resultArray );
287+ return new string (resultArray );
288+ }
289+ }
290+ ```
291+
233292:: tabs-end
234293
235294### Time & Space Complexity
0 commit comments