-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAddBinary.java
More file actions
24 lines (24 loc) · 830 Bytes
/
AddBinary.java
File metadata and controls
24 lines (24 loc) · 830 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//O(N), N is the largest length of a and b
public class Solution {
public String addBinary(String a, String b) {
int carry = 0;
StringBuilder result = new StringBuilder();
for (int i = 0; i < Math.max(a.length(), b.length()); ++i) {
int x = 0, y = 0;
if (i < a.length()) {
x = Character.getNumericValue(a.charAt(a.length() - i - 1));
}
if (i < b.length()) {
y = Character.getNumericValue(b.charAt(b.length() - i - 1));
}
int sum = x + y + carry;
int digit = sum % 2;
result.append(digit);
carry = sum / 2;
}
if (carry != 0) {
result.append(carry);
}
return result.reverse().toString();
}
}