###### 不忘初心 方得始终

Given two binary strings, return their sum (also a binary string).

For example,
a = "11"
b = "1"
Return "100".

public class Solution {
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;

}
}

#### Add Binary | leetcode 67 【Java解题报告】

2015-10-04 14:23:39

#### leetcode 67 Add Binary C++

2016-06-13 10:54:12

2015-07-05 21:49:19

2015-08-19 20:58:22

#### 【leetcode】67. Add Binary（Python & C++）

2017-09-04 20:18:41

2014-11-06 21:36:03

2017-07-17 20:22:34

#### LeetCode 67 : Add Binary (Java)

2015-12-03 21:08:20

#### 67. Add Binary [easy] (Python)

2016-06-18 17:29:55

#### 67. Add Binary Leetcode Python

2015-02-04 23:43:05