My Java Solution


  • 0
    public String addBinary(String a, String b) {
        StringBuilder sb = new StringBuilder();
        int indexA = a.length()-1, indexB = b.length()-1, carry = 0;
        while(indexA >= 0 || indexB >= 0) {
            int tempA = indexA>=0 ? Character.getNumericValue(a.charAt(indexA)) : 0;
            int tempB = indexB>=0 ? Character.getNumericValue(b.charAt(indexB)) : 0;
            int tempSum = (tempA + tempB + carry);
            sb.insert(0, tempSum % 2);
            carry = tempSum / 2;
            indexA--;
            indexB--;
        }
        if(carry!=0) sb.insert(0, carry);
        return sb.toString();
    }
    

    Main idea: this solution is similar to the sum-2 problem, but just replace decimal with binary.


Log in to reply
 

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