Monday, April 6, 2015

Add Binary

Given two binary strings, return their sum (also a binary string).
For example,
a = "11"
b = "1"
Return "100".
Solution:
两个字符串从最后一位开始加,如果遇见长度不一的情况,就把短的字符串高位补0.
每轮计算要加上进位,最后跳出循环后要检查进位是否为1,如果为1,别忘了加在结果的最左位。
public class Solution {
    public String addBinary(String a, String b) {
        int aLen = a.length();
        int bLen = b.length();
        int len = Math.max(aLen, bLen);
        int carry = 0;
        StringBuilder res = new StringBuilder();
        
        for(int i = 0; i < len; i++) {
            int aDigit = 0, bDigit = 0;
            if(i < aLen) {
                aDigit = a.charAt(aLen - i - 1) - '0';
            }else {
                aDigit = 0;
            }
            if(i < bLen) {
                bDigit = b.charAt(bLen - i - 1) - '0';
            }else {
                bDigit = 0;
            }
            
            int temp = aDigit + bDigit + carry;
            carry = temp / 2;
            res.insert(0, Integer.toString(temp % 2));  //remember to insert in the front!
        }
        
        if(carry == 0) {
            return res.toString();
        }else {
            res.insert(0, Integer.toString(1));
            return res.toString();
        }
        
    }
}
Or
Main idea is 2 steps, 1. Check the length of each string and add "0" to the shorter one. 2.Add the two string, using a carry .
public class Solution {
    public String addBinary(String a, String b) {
        if(a == null || a.length() == 0)
            return b;
        else if(b == null || b.length() == 0)
            return a;
        
        int alength = a.length();
        int blength = b.length();
        int length;
        if(alength >= blength) {
            for(int i = 0; i < (alength - blength); i++) {
                b = "0" + b;
            }
            length = alength;
        }else {
            for(int i = 0; i < (blength - alength); i++) {
                a = "0" + a;
            }
            length = blength;
        }
        
        String result = "";
        boolean carry = false;
        
        for(int i = length - 1; i >= 0; i--) {
            if(a.charAt(i) == '1' && b.charAt(i) == '1') {
                if(carry) {
                    result = "1" + result;
                    carry = true;
                }else if(!carry) {
                    result = "0" + result;
                    carry = true;
                }
            }else if(a.charAt(i) == '1' && b.charAt(i) == '0') {
               if(carry) {
                   result = "0" + result;
                   carry = true;
               }else if(!carry) {
                   result = "1" + result;
                   carry = false;
               } 
            }else if(a.charAt(i) == '0' && b.charAt(i) == '1') {
                if(carry) {
                   result = "0" + result;
                   carry = true;
               }else if(!carry) {
                   result = "1" + result;
                   carry = false;
               } 
            }else if(a.charAt(i) == '0' && b.charAt(i) == '0') {
                if(carry) {
                   result = "1" + result;
                   carry = false;
               }else if(!carry) {
                   result = "0" + result;
                   carry = false;
               }  
            }
        }
        if(carry)
            result = "1" + result;
        
        return result;
    }
}

No comments:

Post a Comment