My 3ms Java solution use StringBuilder may help u


  • 2
    B
    public class Solution {
        public String addBinary(String a, String b) {
            StringBuilder sb = new StringBuilder();
    		int carry = 0, i = a.length(), j = b.length();
    		while (i > 0 || j > 0 || carry == 1) {
    			if (i > 0)
    				carry += a.charAt(--i) - '0';
    			if (j > 0)
    				carry += b.charAt(--j) - '0';
    			sb.append((char) ((carry % 2) + '0'));
    			carry >>>= 1;
    		}
    		return sb.reverse().toString();
    	}
    }

Log in to reply
 

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