[LeetCode]Add Binary,leetcodeaddbinary

來源:互聯網
上載者:User

[LeetCode]Add Binary,leetcodeaddbinary

題目:給定兩個位元字字串A、B,計算出A+B並返回和值的字串

演算法一:最原始的辦法,類比二進位的運算,因考慮到給定的二進位字串很長,所以使用Java的BigInteger實現

import java.math.BigInteger;public class Solution {    /**     * Convert binary number to BigInteger.     * @param binary     * @return     */    public BigInteger convertBinaryToBigInteger(String binary) {        BigInteger factor  = BigInteger.ONE;        BigInteger convert = BigInteger.ZERO;                int length = binary.length();        for (int i=length-1; i>=0; --i) {            long num = (long)(binary.charAt(i)-'0');            convert = convert.add(factor.multiply(BigInteger.valueOf(num)));                        factor = factor.shiftLeft(1);        }        return convert;    }        /**     * Convert BigInteger to binary number.     * @param c     * @return     */    public String convertBigIntegerToBinary(BigInteger c) {        if (c.equals(BigInteger.ZERO)) {            return "0";        }                String res = new String();        BigInteger factor = BigInteger.valueOf(2L);        while (!c.equals(BigInteger.ZERO)) {            res += c.mod(factor).toString();            c = c.divide(factor);        }                return new StringBuffer(res).reverse().toString();     }        /**     * Add Two given binary number and return the binary number of their sum.     * @param a     * @param b     * @return     */    public String addBinary(String a, String b) {        BigInteger aBigInteger = convertBinaryToBigInteger(a);        BigInteger bBigInteger = convertBinaryToBigInteger(b);                return convertBigIntegerToBinary(aBigInteger.add(bBigInteger));    }}

演算法二:利用異或運算,這種方法更加優雅

public String addBinary(String a, String b) {        String convert = new String();        char[] aArray = a.toCharArray();        char[] bArray = b.toCharArray();                int aByte  = 0;        int bByte  = 0;        int carry  = 0;        int aIndex = aArray.length - 1;        int bIndex = bArray.length - 1;        while (aIndex>-1 || bIndex>-1 || carry>0) {            aByte = aIndex > -1 ? Character.getNumericValue(aArray[aIndex--]) : 0;            bByte = bIndex > -1 ? Character.getNumericValue(bArray[bIndex--]) : 0;                        convert += String.valueOf(aByte ^ bByte ^ carry);            carry = (aByte + bByte + carry) > 1 ? 1 : 0;        }// end of while                return new StringBuffer(convert).reverse().toString();    }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.