67. Add Binary

题目:

Given two binary strings, return their sum (also a binary string).

For example,
a = "11"
b = "1"
Return "100".

链接:  http://leetcode.com/problems/add-binary/

题解:

Add two binary strings,从后向前加即可,最后判断一下carry = 1的case。 这种code,算法其实不重要,还要多思考多练写得比较concise才好。要手熟。

Time Complexity - O(n), Space Complextiy O(1)

public class Solution {
    public String addBinary(String a, String b) {
        if(a == null || b == null)
            return "0";
        int aIndex = a.length() - 1, bIndex = b.length() - 1;
        int carry = 0, curDigit = 0;
        StringBuilder sb = new StringBuilder();
        
        while(aIndex >= 0 || bIndex >= 0) {
            int aValue = aIndex >= 0 ? a.charAt(aIndex) - '0' : 0;
            int bValue = bIndex >= 0 ? b.charAt(bIndex) - '0' : 0;
            curDigit = (aValue + bValue + carry) % 2;
            sb.insert(0, curDigit);
            carry = (aValue + bValue + carry) >= 2 ? 1 : 0;
            aIndex--;
            bIndex--;
        }
        
        if(carry == 1)
            sb.insert(0, "1");
        
        return sb.toString();
    }
}

 

二刷:

Java:

使用或,使用sb.insert(0, curDigit)。 

Time Complexity - O(n2), Space Complexity - O(n)

public class Solution {
    public String addBinary(String a, String b) {
        if (a == null || b == null) {
            return "0";
        }
        int aHi = a.length() - 1, bHi = b.length() - 1;
        StringBuilder sb = new StringBuilder();
        int carry = 0;
        while (aHi >= 0 || bHi >= 0) {
            int digitA = aHi >= 0 ? a.charAt(aHi) - '0' : 0;
            int digitB = bHi >= 0 ? b.charAt(bHi) - '0' : 0;
            sb.insert(0, (digitA + digitB + carry) % 2);
            carry = (digitA + digitB + carry) >= 2 ? 1 : 0;
            aHi--;
            bHi--;
        }
        if (carry == 1) {
            sb.insert(0, 1);
        }
        return sb.toString();
    }
}

 

这样写快一点点:

先append, 再reverse来一遍

public class Solution {
    public String addBinary(String a, String b) {
        if (a == null || b == null) {
            return "0";
        }
        int aHi = a.length() - 1, bHi = b.length() - 1;
        StringBuilder sb = new StringBuilder();
        int carry = 0;
        while (aHi >= 0 || bHi >= 0) {
            int digitA = aHi >= 0 ? a.charAt(aHi) - '0' : 0;
            int digitB = bHi >= 0 ? b.charAt(bHi) - '0' : 0;
            sb.append((digitA + digitB + carry) % 2);
            carry = (digitA + digitB + carry) >= 2 ? 1 : 0;
            aHi--;
            bHi--;
        }
        if (carry == 1) {
            sb.append(1);
        }
        String res = sb.toString();
        sb.setLength(0);
        for (int i = res.length() - 1; i >= 0; i--) {
            sb.append(res.charAt(i));
        }
        return sb.toString();
    }
}

 

三刷:

Java:

public class Solution {
    public String addBinary(String a, String b) {
        if (a == null || b == null) return "0";
        int i = a.length() - 1, j = b.length() - 1;
        StringBuilder sb = new StringBuilder();
        int carry = 0;
        while (i >= 0 || j >= 0) {
            int digitA = i >= 0 ? a.charAt(i) - '0' : 0;
            int digitB = j >= 0 ? b.charAt(j) - '0' : 0;
            sb.append((digitA + digitB + carry) % 2);
            carry = (digitA + digitB + carry) >= 2 ? 1 : 0;
            i--;
            j--;
        }
        if (carry == 1) sb.append(1);
        String res = sb.toString();
        sb.setLength(0);
        for (int k = res.length() - 1; k >= 0; k--) sb.append(res.charAt(k));
        return sb.toString();
    }
}

 

Update:

使用一个char[] array来保存数组,最后遍历char[] array,把每一个数字放在StringBuilder里,最后返回结果。

public class Solution {
    public String addBinary(String a, String b) {
        if (a == null || b == null) return "";
        int i = a.length() - 1, j = b.length() - 1;
        int[] digits = new int[i + j + 2];
        int k = digits.length - 1;
        int carry = 0;
        while (i >= 0 || j >= 0) {
            int digitA = (i >= 0) ? (a.charAt(i) - '0') : 0;
            int digitB = (j >= 0) ? (b.charAt(j) - '0') : 0;
            digits[k--] = (digitA + digitB + carry) % 2;
            carry =  (digitA + digitB + carry >= 2) ? 1 : 0;
            i--;
            j--;
        }
        if (carry == 1) digits[k] = 1;
        StringBuilder sb = new StringBuilder();
        for (int digit : digits) {
            if (digit == 0 && sb.length() == 0) continue;
            sb.append(digit);
        }
        if (sb.length() == 0) return "0";
        return sb.toString();
    }
}

 

posted @ 2015-04-18 00:08  YRB  阅读(406)  评论(0编辑  收藏  举报