67. Add Binary

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

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

class Solution {
public:
    string addBinary(string a, string b) {
        int aLen = a.length();
        int bLen = b.length();
        bool carry = false;
        int aPointer = aLen-1;
        int bPointer = bLen-1;
        if(aLen >= bLen){
            while(bPointer >=0)
            {
                if(!carry){
                    if(a[aPointer] == '0' && b[bPointer] == '0')
                    {
                        a[aPointer] = '0';
                    }
                    else if(a[aPointer] == '1' && b[bPointer] == '1')
                    {
                        a[aPointer] = '0';
                        carry = true;
                    }
                    else
                    {
                        a[aPointer] = '1';
                    }
                }
                else
                {
                    if(a[aPointer] == '0' && b[bPointer] == '0')
                    {
                        a[aPointer] = '1';
                        carry = false;
                    }
                    else if(a[aPointer] == '1' && b[bPointer] == '1')
                    {
                        a[aPointer] = '1';
                    }
                    else
                    {
                        a[aPointer] = '0';
                    }
                }
                aPointer--;
                bPointer--;
            }
            while(aPointer>=0 && carry)
            {
                if(a[aPointer] == '0')
                {
                    a[aPointer] = '1';
                    carry = false;
                    break;
                }
                a[aPointer]='0';
                aPointer--;
            }
            if(carry)
            {
                a = "1" + a;
            }
            return a;
        }
        else{
            while(aPointer >=0)
            {
                if(!carry){
                    if(a[aPointer] == '0' && b[bPointer] == '0')
                    {
                        b[bPointer] = '0';
                    }
                    else if(a[aPointer] == '1' && b[bPointer] == '1')
                    {
                        b[bPointer] = '0';
                        carry = true;
                    }
                    else
                    {
                        b[bPointer] = '1';
                    }
                }
                else
                {
                    if(a[aPointer] == '0' && b[bPointer] == '0')
                    {
                        b[bPointer] = '1';
                        carry = false;
                    }
                    else if(a[aPointer] == '1' && b[bPointer] == '1')
                    {
                        b[bPointer] = '1';
                    }
                    else
                    {
                        b[bPointer] = '0';
                    }
                }
                aPointer--;
                bPointer--;
            }
            while(bPointer>=0 && carry)
            {
                if(b[bPointer] == '0')
                {
                    b[bPointer] = '1';
                    carry = false;
                    break;
                }
                b[bPointer] = '0';
                bPointer--;
            }
            if(carry)
            {
                b = "1" + b;
            }
            return b;
        }
    }
};

 

posted on 2015-11-02 14:58  joannae  阅读(100)  评论(0编辑  收藏  举报

导航