File tree Expand file tree Collapse file tree 2 files changed +19
-1
lines changed
July-LeetCoding-Challenge/19-Add-Binary Expand file tree Collapse file tree 2 files changed +19
-1
lines changed Original file line number Diff line number Diff line change
1
+ public class Solution {
2
+ public string AddBinary ( string a , string b )
3
+ {
4
+ var sb = new StringBuilder ( ) ;
5
+ int i = a . Length - 1 , j = b . Length - 1 , carry = 0 ;
6
+ while ( i >= 0 || j >= 0 ) {
7
+ int sum = carry ;
8
+ if ( i >= 0 ) sum += a [ i -- ] - '0' ;
9
+ if ( j >= 0 ) sum += b [ j -- ] - '0' ;
10
+ sb . Append ( sum % 2 ) ;
11
+ carry = sum / 2 ;
12
+ }
13
+ if ( carry != 0 ) sb . Append ( carry ) ;
14
+ var arr = sb . ToString ( ) . ToCharArray ( ) ;
15
+ Array . Reverse ( arr ) ;
16
+ return new String ( arr ) ;
17
+ }
18
+ }
Original file line number Diff line number Diff line change @@ -25,7 +25,7 @@ Solutions in various programming languages are provided. Enjoy it.
25
25
16 . [ Pow(x,n)] ( https://github.com/AlgoStudyGroup/Leetcode/tree/master/July-LeetCoding-Challenge/16-Pow(x,n) ) : Recursion
26
26
17 . [ Top K Frequent Elements] ( https://github.com/AlgoStudyGroup/Leetcode/tree/master/July-LeetCoding-Challenge/17-Top-K-Frequent-Elements ) : Bucket
27
27
18 . [ Course Schedule II] ( https://github.com/AlgoStudyGroup/Leetcode/tree/master/July-LeetCoding-Challenge/18-Course-Schedule-II ) : BFS
28
-
28
+ 19 . [ Add Binary ] ( https://github.com/AlgoStudyGroup/Leetcode/tree/master/July-LeetCoding-Challenge/19-Add-Binary )
29
29
30
30
## June LeetCoding Challenge
31
31
Click [ here] ( https://leetcode.com/explore/challenge/card/june-leetcoding-challenge/ ) for problem descriptions.
You can’t perform that action at this time.
0 commit comments