Java one line solution using BigInteger

  • -1

    You may consider this is cheating. But performance is great(~230ms) and I found it interesting to discover how to deal with numbers larger than long.

    The idea is simple. Covert string to decimal, add, convert back to binary string.

    The thing is I always see one line python solution but rarely get a Java solution that is this pretty. However the only discussion about BigInteger gets down votes I think it is quite unfair.

    import java.math.BigInteger;
    public class Solution {
        public String addBinary(String a, String b) {
            return (new BigInteger(a,2)).add(new BigInteger(b,2)).toString(2);

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.