67 Add Binary – Easy
Problem:
Thoughts:
Solutions:
public class Solution {
public String addBinary(String a, String b) {
String result = "";
int i = a.length() - 1, j = b.length() - 1;
int carry = 0;
while (i >=0 || j >=0) {
int tmp = (i >=0?(a.charAt(i)- '0'):0 ) + (j >=0?(b.charAt(j) - '0'):0) + carry;
carry = tmp / 2;
int digit = tmp % 2;
result = digit + result;
i --;
j --;
}
if (carry > 0) {
result = carry + result;
}
return result;
}
}Last updated