-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
30 lines (30 loc) · 1.09 KB
/
Solution.java
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
class Solution {
public String addBinary(String a, String b) {
String ra = new StringBuffer(a).reverse().toString(); // reverse String a
String rb = new StringBuffer(b).reverse().toString(); // reverse String b
String res = "";
if(ra.length() < rb.length()) { // make sure the length of ra is larger than or equal to rb
String tmp = rb;
rb = ra;
ra = tmp;
}
int pointer = 0;
int cb = 0; // carry bit
while(pointer < ra.length()) {
int aa = ra.charAt(pointer) -'0'; // get the number at current position of ra
int bb;
if(pointer < rb.length()) {
bb = rb.charAt(pointer) -'0'; // get the number at current position of rb
} else {
bb = 0; // set the number at current position of rb to 0
}
res += (aa + bb + cb) % 2;
cb = (aa + bb + cb) / 2;
pointer++;
}
if(cb != 0) {
res += cb;
}
return new StringBuffer(res).reverse().toString();
}
}