67. Add Binary

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

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

此题主要考察数学知识,这里面注意,如果String的值要不断的改变的话,就最好用StringBuilder,我用了String结果没有通过,代码如下:

public class Solution {

    public String addBinary(String a, String b) {

        int i = a.length()-1;

        int j = b.length()-1;

        int c = 0;

        String s = "";

        while(i>=0||j>=0||c==1){

            if(i>=0) c+=a.charAt(i--)-'0';

            if(j>=0) c+=b.charAt(j--)-'0';

            sb.append(c%2);

            c/=2;

        }

        return sb.reverse().toString();

    }

}

原文地址:https://www.cnblogs.com/codeskiller/p/6358600.html