A 8-Line C# Solution to have all languages here


  • 3
    L
    public string AddBinary(string a, string b) {
        StringBuilder sb = new StringBuilder();
        int carry = 0;
        for (int i = a.Length - 1, j = b.Length - 1; i >= 0 || j >= 0; i--, j--){
            int total= ((i >= 0 ? a[i] - '0' : 0) + (j >= 0 ? b[j] - '0' : 0)) + carry;
            sb.Insert(0, total == 2 || total == 0 ? '0' : '1');
            carry = total > 1 ? 1 : 0;
        }
        return carry == 1 ? sb.Insert(0, 1).ToString() : sb.ToString();
    }

  • 0
    C

    My C# code is longer and it exceeds memory limits.


  • 0
    L

    I rewrote the code and it is more readable.


  • 0
    E

    Clean. The easiest solution to understand.


Log in to reply
 

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