-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathAdd Binary.java
40 lines (40 loc) · 1.06 KB
/
Add Binary.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
public class Solution {
/**
* @param a a number
* @param b a number
* @return the result
*/
public String addBinary(String a, String b) {
// Write your code here
if (a == null || b == null) {
return a;
}
if (a.length() == 0 || b.length() == 0) {
return a.length() == 0 ? b : a;
}
if (a.length() < b.length()) {
String temp = b;
b = a;
a = temp;
}
int carry = 0;
int aI = a.length() - 1;
int bI = b.length() - 1;
String rst = "";
while (bI >= 0) {
int temp = (a.charAt(aI--) - '0') + (b.charAt(bI--) - '0') + carry;
carry = temp / 2;
rst = String.valueOf(temp % 2) + rst;
}
while (aI >= 0) {
int temp = a.charAt(aI--) - '0' + carry;
carry = temp / 2;
rst = String.valueOf(temp % 2) + rst;
}
if (carry == 1) {
return "1" + rst;
} else {
return rst;
}
}
}