-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathAddBinary.java
42 lines (34 loc) · 1.03 KB
/
AddBinary.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
31
32
33
34
35
36
37
38
39
40
41
42
package problems.leetcode;
/**
* https://leetcode.com/problems/add-binary/
*/
public class AddBinary {
public static void main(String[] args) {
String a = "11";
String b = "11";
System.out.println(addBinary(a, b));
}
public static String addBinary(String a, String b) {
StringBuilder result = new StringBuilder();
int carry = 0;
for (int i = 0, len = Math.max(a.length(), b.length()); i < len; i++) {
int na = i < a.length() ? a.charAt(a.length() - 1 - i) - '0' : 0;
int nb = i < b.length() ? b.charAt(b.length() - 1 - i) - '0' : 0;
int sum = na + nb + carry;
if (sum == 2) {
sum = 0;
carry = 1;
} else if (sum == 3) {
sum = 1;
carry = 1;
} else {
carry = 0;
}
result.append(sum);
}
if (carry > 0) {
result.append(carry);
}
return result.reverse().toString();
}
}