67. Add Binary - easy

bit by bit的相加,a + b + c的形式
这道题并不难,很多小细节需要注意。
比如:

c += (i>0)? a.charAt(i-1)-'0' : 0;

写成以下形式是不行的,必须跟着等号。

c = c + (i>0)? a.charAt(i-1)-'0' : 0;

另外注意,int和char的混用。
my code:

public class Solution {
    public String addBinary(String a, String b) {
        int i = a.length(), j = b.length(), c=0;
        String s = "";
        while(i>0 || j>0 || c>0) {
            c += (i>0)? a.charAt(i-1)-'0' : 0;
            c += (j>0)? b.charAt(j-1)-'0' : 0;
            s = c%2 + s;
            c = c/2;
            i--; j--;
        }
        return s;
    }
}

Just focus on it. Don't think about other things. --- 10/19/2016

你可能感兴趣的:(67. Add Binary - easy)