-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd binary.java
43 lines (35 loc) · 1.14 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
41
42
43
/*
this problem is simple compared with multiple string
we don't need extra array to store the result when conducting an operation
and also bit-bit process the binary
attention:
it is a way to reverse the string first and then do the operation as well
*/
public class Solution {
public String addBinary(String a, String b) {
int aLen = a.length();
int bLen = b.length();
String res = "";
int maxLen = Math.max(aLen, bLen);
int carry = 0;// carry will be update in each iteration
for(int i = 0; i < maxLen; i++){
int currA = 0;
int currB = 0;
if(i < aLen){
currA = a.charAt(aLen - 1 - i) - '0';
}else{
currA = 0;
}
if(i < bLen){
currB = b.charAt(bLen - 1 - i) - '0';
}else{
currB = 0;
}
int tmp = currA + currB + carry;
carry = tmp / 2;
res = tmp % 2 + res;
}
// the final result could be more than the maxlen
return (carry == 0) ? res : "1" + res ;
}
}