Java 1 line solution


  • -46
    M
    public class Solution { public String addBinary(String a, String b) { StringBuilder sum = new StringBuilder(); int aLength = a.length(); int bLength = b.length(); int min = Math.min(aLength, bLength); boolean carry = false; for (int i = 0; i < min; i++) { char aC = a.charAt(aLength - i - 1); char bC = b.charAt(bLength - i - 1); if (aC == '0' && bC == '0') { if (carry) { sum.append('1'); carry = false; } else { sum.append('0'); } } else if (aC == '1' && bC == '1') { if (carry) { sum.append('1'); } else { sum.append('0'); carry = true; } } else { if (carry) { sum.append('0'); } else { sum.append('1'); } } } String rest; if (min == aLength) { rest = b; } else { rest = a; } for (int i = 0; i < rest.length() - min; i++) { char c = rest.charAt(rest.length() - min - i - 1); if (c == '1') { if (carry) { sum.append('0'); } else { sum.append('1'); carry = false; } } else { if (carry) { sum.append('1'); carry = false; } else { sum.append('0'); } } } if (carry) sum.append('1'); return sum.reverse().toString(); } }
    

    What do you guys think?


  • 0
    S

    lol...it is -19 now


  • 1
    L

    This guy is just make a joke,lol. It's really funny.


  • 0
    H

    lol, I down voted you, it's still funny though


Log in to reply
 

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