BiruLyu
5/23/2017 - 6:49 PM

67. Add Binary(#1).java

"""
Input:
"0"
"0"
"11"
"1"
"1110"
"1"
"1011"
"1011"

Output:
"0"
"100"
"1111"
"10110"
"""
class Solution(object):
    def addBinary(self, a, b):
        """
        :type a: str
        :type b: str
        :rtype: str
        """
        
        res = "";
        flag = 0;
        while(a or b):
            if a and b:
                digit1 = a[-1];
                a = a[:-1];
                digit2 = b[-1];
                b = b[:-1];
            elif not a:
                digit1 = 0;
                digit2 = b[-1];
                b = b[:-1];
            elif not b:
                digit1 = a[-1];
                a = a[:-1];
                digit2 = 0;
                
            temp = int(digit1) + int(digit2) + flag;
            if temp > 1:
                flag = 1;
                res += str(temp % 2);
            else:
                res += str(temp);
                flag = 0; #Node that after using flag, should update it
        
        if flag == 1:
            res += '1';
        return res[::-1];
public class Solution {
    public String addBinary(String a, String b) {
        int flag = 0;
        StringBuilder builder = new StringBuilder();
        int len1 = a.length();
        int len2 = b.length();
        while(len1 != 0 || len2 != 0){
            char temp1 = '0';
            char temp2 = '0';
            char tempSum = '0';
            if(len1 != 0 && len2 != 0){
                temp1 = a.charAt( len1 - 1);
                temp2 = b.charAt( len2 - 1);
                len1--;
                len2--;
            } else if( len1 != 0){
                temp1 = a.charAt( len1 - 1);
                len1--;
                temp2 = '0';
            } else if( len2 != 0){
                temp1 = '0';
                temp2 = b.charAt( len2 - 1);
                len2--;
            }
            
            if(temp1 == '1' && temp2 == '1'){
            	tempSum = flag == 1 ? '1' : '0';
                flag = 1;   
            } else if(temp1 == '1' || temp2 == '1'){
                if(flag == 1){
                	tempSum = '0';
                	flag = 1;
                } else {
                	tempSum = '1';
                }
            } else {
            	if(flag == 1){
            		tempSum = '1';
            		flag = 0;
            	} else {
            		tempSum = '0';
            	}
            }
           
            builder.append(tempSum);
        }
        if(flag == 1) builder.append('1');
        
        return builder.reverse().toString();
    }
}


/*
Input:
"0"
"0"
"11"
"1"
"1110"
"1"
"1011"
"1011"
"1010"
"1011"

Output:
"0"
"100"
"1111"
"10110"
"10101"
*/
public class Solution {
    public String addBinary(String a, String b) {
        StringBuilder sb = new StringBuilder();
        int i = a.length() - 1, j = b.length() -1, carry = 0;
        while (i >= 0 || j >= 0) {
            int sum = carry;
            if (j >= 0) sum += b.charAt(j--) - '0';
            if (i >= 0) sum += a.charAt(i--) - '0';
            sb.append(sum % 2);
            carry = sum / 2;
        }
        if (carry != 0) sb.append(carry);
        return sb.reverse().toString();
    }
}