Python one-liner (100% of solutions beat? A little odd)


  • 0
    G

    Not sure why, I always thought format was slower than bin, but here:

    class Solution(object):
        def addBinary(self, a, b):
            return "{:b}".format(int(a, 2)+int(b, 2))

  • 0

    I think you were just lucky. Try submitting a few more times. Or try this:

    def addBinary(self, a, b):
        for _ in range(1000):
            result = "{:b}".format(int(a, 2)+int(b, 2))
            #result = bin(int(a, 2)+int(b, 2))[2:]
        return result
    

    I submitted a few times and with format this typically took about 480ms and with bin it typically took about 440ms. Also, this shows that the solution's runtime without blowing it up by a factor of 1000 is only about 0.4ms, and the remaining 99% of your 40ms is the judge overhead. The judge's variance is way more significant and the solution's own runtime doesn't matter unless it's a lot slower.

    Sorry :-P


  • 0
    G

    Yeah, as expected. I wonder why OJ doesn't run enough tests to remove the judge's overhead.


Log in to reply
 

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