Given two binary strings, return their sum (also a binary string). For example, given a = "11" and b = "1", return "100".
http://www.jiuzhang.com/solutions/add-binary/
Use String Builder
http://huntfor.iteye.com/blog/2061827 ????
Read full article from LeetCode - Add Binary | Darren's Blog
public String addBinary(String a, String b) {
if(a == null || a.isEmpty()) {
return b;
}
if(b == null || b.isEmpty()) {
return a;
}
char[] aArray = a.toCharArray();
char[] bArray = b.toCharArray();
StringBuilder stb = new StringBuilder();
int i = aArray.length - 1;
int j = bArray.length - 1;
int aByte;
int bByte;
int carry = 0;
int result;
while(i > -1 || j > -1 || carry == 1) {
aByte = (i > -1) ? Character.getNumericValue(aArray[i--]) : 0;
bByte = (j > -1) ? Character.getNumericValue(bArray[j--]) : 0;
result = aByte ^ bByte ^ carry;
carry = ((aByte + bByte + carry) >= 2) ? 1 : 0;
stb.append(result);
}
return stb.reverse().toString();
}
public String addBinary(String a, String b) {
if(a == null || a.isEmpty())
return b;
if(b == null || b.isEmpty())
return a;
StringBuilder stb = new StringBuilder();
int i = a.length() - 1;
int j = b.length() - 1;
int aBit;
int bBit;
int carry = 0;
int result;
while(i >= 0 || j >= 0 || carry == 1) {
aBit = (i >= 0) ? Character.getNumericValue(a.charAt(i--)) : 0;
bBit = (j >= 0) ? Character.getNumericValue(b.charAt(j--)) : 0;
result = aBit ^ bBit ^ carry;
carry = ((aBit + bBit + carry) >= 2) ? 1 : 0;
stb.append(result);
}
return stb.reverse().toString();
}
public String addBinary(String a, String b) {
StringBuilder sb = new StringBuilder();
int i = a.length() - 1, j = b.length() -1, carry = 0;
while (i >= 0 || j >= 0) {
int sum = carry;
if (j >= 0) sum += b.charAt(j--) - '0';
if (i >= 0) sum += a.charAt(i--) - '0';
sb.append(sum % 2);
carry = sum / 2;
}
if (carry != 0) sb.append(carry);
return sb.reverse().toString();
}
public String addBinary(String a, String b) {
int i = a.length() - 1;
int j = b.length() - 1;
int carry = 0;
StringBuilder sb = new StringBuilder();
while (i >= 0 || j >= 0 || carry > 0){
int sum = ((i < 0) ? 0 : (a.charAt(i--) - '0')) +
((j < 0) ? 0 : (b.charAt(j--) - '0')) +
carry;
sb.append(Integer.toString(sum & 1));
carry = (sum >> 1) & 1;
}
return sb.reverse().toString();
}
http://blog.welkinlan.com/2015/10/23/add-binary-leetcode-java/
public String addBinary(String a, String b) {
int m = a.length(), n = b.length();
StringBuilder result = new StringBuilder();
int carry = 0;
int i = m-1, j = n-1;
// Sum the two digits at the same position and the carry, and get the
// resulting digit in that position, and update the carry
for (; i >= 0 && j >= 0; i--, j--) {
switch (a.charAt(i)-'0'+b.charAt(j)-'0'+carry) {
case 0: result.insert(0, 0); carry = 0; break;
case 1: result.insert(0, 1); carry = 0; break;
case 2: result.insert(0, 0); carry = 1; break;
case 3: result.insert(0, 1); carry = 1; break;
}
}
// b has no digit in such significant position
for (; i >= 0; i--) {
switch (a.charAt(i)-'0'+carry) {
case 0: result.insert(0, 0); carry = 0; break;
case 1: result.insert(0, 1); carry = 0; break;
case 2: result.insert(0, 0); carry = 1; break;
}
}
// a has no digit in such sifnificant position
for (; j >= 0; j--) {
switch (b.charAt(j)-'0'+carry) {
case 0: result.insert(0, 0); carry = 0; break;
case 1: result.insert(0, 1); carry = 0; break;
case 2: result.insert(0, 0); carry = 1; break;
}
}
// Another "1" in the most significant position in case carry=1 after the above steps
if (carry == 1)
result.insert(0, 1);
return result.toString();
}
http://www.programcreek.com/2014/05/leetcode-add-binary-java/public String addBinary(String a, String b) { if(a==null || a.length()==0) return b; if(b==null || b.length()==0) return a; int pa = a.length()-1; int pb = b.length()-1; int flag = 0; StringBuilder sb = new StringBuilder(); while(pa >= 0 || pb >=0){ int va = 0; int vb = 0; if(pa >= 0){ va = a.charAt(pa)=='0'? 0 : 1; pa--; } if(pb >= 0){ vb = b.charAt(pb)=='0'? 0: 1; pb--; } int sum = va + vb + flag; if(sum >= 2){ sb.append(String.valueOf(sum-2)); flag = 1; }else{ flag = 0; sb.append(String.valueOf(sum)); } } if(flag == 1){ sb.append("1"); } String reversed = sb.reverse().toString();//\\ return reversed; } |
http://www.jiuzhang.com/solutions/add-binary/
Use String Builder
public String addBinary(String a, String b) { if(a.length() < b.length()){ String tmp = a; a = b; b = tmp; } int pa = a.length()-1; int pb = b.length()-1; int carries = 0; String rst = ""; while(pb >= 0){ int sum = (int)(a.charAt(pa) - '0') + (int)(b.charAt(pb) - '0') + carries; rst = String.valueOf(sum % 2) + rst;//\\ carries = sum / 2; pa --; pb --; } while(pa >= 0){ int sum = (int)(a.charAt(pa) - '0') + carries; rst = String.valueOf(sum % 2) + rst; carries = sum / 2; pa --; } if (carries == 1) rst = "1" + rst; return rst; }
http://huntfor.iteye.com/blog/2061827 ????
- public String addBinary(String a, String b) {
- StringBuilder aReverse = new StringBuilder(a).reverse();
- StringBuilder bReverse = new StringBuilder(b).reverse();
- //进行高位补0操作
- if (a.length() > b.length()) {
- for (int i = 0; i < a.length() - b.length(); i++) {
- bReverse.append("0");
- }
- } else {
- for (int i = 0; i < b.length() - a.length(); i++) {
- aReverse.append("0");
- }
- }
- int tem = 0;
- StringBuilder c = new StringBuilder();
- for (int i = 0; i < aReverse.length(); i++) {
- int sum = (aReverse.charAt(i) - '0') + (bReverse.charAt(i) - '0') + tem;
- if (sum < 2) {
- c.append(sum + "");
- tem = 0;
- } else {//else的情况有两种,sum = 2或3,这里一并处理了
- tem = 1;
- c.append(sum % 2 + "");
- }
- }
- if (tem == 1) {//最高位产生进位
- c.append("1");
- }
- return c.reverse().toString();
- }
Read full article from LeetCode - Add Binary | Darren's Blog