-
Notifications
You must be signed in to change notification settings - Fork 1
/
67AddBinary2.cs
61 lines (54 loc) · 1.72 KB
/
67AddBinary2.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
56
57
58
59
60
61
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _67AddBinary2
{
class Program
{
static void Main(string[] args)
{
}
/*
* source code from blog
* http://fisherlei.blogspot.ca/2013/01/leetcode-add-binary.html
*
* julia's comment:
* Leetcode online judge:
* 294 / 294 test cases passed.
Status: Accepted
Runtime: 128 ms
*
* Write down thought process:
* two string, go through one loop with two indexes, one is for string a, another one is for string b;
* the iteration is from right to left, decreasing order;
* althought two strings with different length, the addition will still continue normally
* considering the carry and one or two numbers from input argument.
* The tip is to set addition integer to 0 if the string index is < 0
*/
string addBinary(string a, string b) {
int carry =0;
string result = "";
int aLen = a.Length;
int bLen = b.Length;
for(int i = aLen - 1, j = bLen - 1; // from rightmost digit to left
i >=0 || j>=0;
--i,--j
)
{
int ai = i>=0? a[i]-'0':0;
int bj = j>=0? b[j]-'0':0;
int val = (ai+bj+carry)%2;
carry = (ai+bj+carry) /2;
char c = (char)(val + '0');
result = c.ToString()+result;
}
if(carry ==1)
{
result = "1" + result; ;
}
return result;
}
}
}