Skip to content

Commit 3fd35d6

Browse files
authored
Merge pull request #227 from hellomrsun/master
Add Add Binary C#
2 parents 1cece77 + 513a3dc commit 3fd35d6

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Solutions in various programming languages are provided. Enjoy it.
2525
16. [Pow(x,n)](https://github.com/AlgoStudyGroup/Leetcode/tree/master/July-LeetCoding-Challenge/16-Pow(x,n)): Recursion
2626
17. [Top K Frequent Elements](https://github.com/AlgoStudyGroup/Leetcode/tree/master/July-LeetCoding-Challenge/17-Top-K-Frequent-Elements): Bucket
2727
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)
2929

3030
## June LeetCoding Challenge
3131
Click [here](https://leetcode.com/explore/challenge/card/june-leetcoding-challenge/) for problem descriptions.

0 commit comments

Comments
 (0)