Given two binary strings, return their sum (also a binary string).
For example,
a = "11"
b = "1"
Return "100"
.
<span style="font-size:14px;">public class Solution {
public String addBinary(String a, String b) {
String res="";
int n1=a.length()-1;
int n2=b.length()-1;
int temp=0;
while(n1>=0&&n2>=0){
temp+=(a.charAt(n1)-'0')+(b.charAt(n2)-'0');
res=temp%2+res;
temp/=2;
n1--;
n2--;
}
while(n1>=0){
temp+=(a.charAt(n1)-'0');
res=temp%2+res;
temp/=2;
n1--;
}
while(n2>=0){
temp+=(b.charAt(n2)-'0');
res=temp%2+res;
temp/=2;
n2--;
}
if(temp==1)
res="1"+res;
return res;
}
}</span>