-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdd Binary
More file actions
33 lines (32 loc) · 847 Bytes
/
Add Binary
File metadata and controls
33 lines (32 loc) · 847 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
25
26
27
28
29
30
31
32
33
public class Solution {
public String addBinary(String a, String b) {
StringBuilder result = new StringBuilder();
int i = a.length() - 1;
int j = b.length() - 1;
int temp = 0;
char m = '0';
char n = '0';
while(i >= 0 || j >= 0){
if(i < 0 && j >= 0){
m = '0';
n = b.charAt(j);
}
else if(j < 0 && i >= 0){
n = '0';
m = a.charAt(i);
}
else{
m = a.charAt(i);
n = b.charAt(j);
}
result.insert(0, (m + n + temp) % 2);
temp = (Character.getNumericValue(m) + Character.getNumericValue(n) + temp)/ 2; // change char to int!!!
i--;
j--;
}
if(temp == 1){
result.insert(0, 1);
}
return result.toString();
}
}