zoukankan      html  css  js  c++  java
  • [LeetCode]Add Binary

    题目:给定两个二进制数字字符串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();
        }

  • 相关阅读:
    Navicat Premium12以上版本多用户破解方法
    Linux并行gzip压缩工具pigz
    Windows Server 2019远程桌面服务配置和授权激活
    mysql删除大表
    KVM qcow2 磁盘在线扩容方法
    在jenkins中连接kubernetes集群
    CentOS 7部署 Ceph分布式存储架构
    (转)关于T(n) = kT(n/c) + f(n) 的时间复杂度
    算法中的思想(第0篇)
    (求通俗易懂的证法) 过n个有标志顶点的树的数目等于n^(n-2)
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/4302246.html
Copyright © 2011-2022 走看看