My C++ Solution Using many tricks as possible! Very Clean!


  • 0

    Thanks to former sharers. I use my tricks as many as possible to improve the code.
    The the below code occurs.

    Try to appreciate this code.

    string addBinary(string a, string b)
    	{
    		string s;
    		s.reserve(a.size() + b.size());
    		int c = 0, i = a.size() - 1, j = b.size() - 1;
    		while(i >= 0 || j >= 0 || c == 1)
    		{
    			c += i >= 0 ? a[i--] - '0' : 0;
    			c += j >= 0 ? b[j--] - '0' : 0;
    			s.push_back((c & 1) + '0');
    			c >>= 1;
    		}
    		reverse(s.begin(), s.end());
    		return s;
    	}
    

Log in to reply
 

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