C++


  • 0
    B

    class Solution {
    public:
    string addBinary(string a, string b) {
    string result = "";
    int c = 0;
    int i = a.size() - 1;
    int j = b.size() - 1;

        while(i >= 0 || j >=0 ||c ==1)  
        {  
            if(i>=0)
            c += (a[i--]-'0');
            if(j>=0)
            c += (b[j--]-'0');
            result = char( c% 2 + '0') + result;  
            c /= 2;  
        }  
          
        return result;  
          
    
    }
    

    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.