Is this cheating?


  • 0
    Z
    class Solution:
        # @param a, a string
        # @param b, a string
        # @return a string
        def addBinary(self, a, b):
            total=""
            sum = bin(int(a,2)+int(b,2))
            for i in range(2,len(sum)):
                total += str(sum[i])
            return total 
    

    The solution was accepted by OJ, but I am wondering if it's cheating since I used build in function from python. Thanks in advance!


  • 21
    S

    It is cheating only when your interviewer thinks so. However, if you give such a solution, the interviewer will probably ask you to implement the building function(s). Remember the point is not to pass the OJ, but to demonstrate that you have understanding of algorithms or data structures. Your implement shows nothing about your understanding of element-wise addition, so it may not be what the interviewer is looking for.


  • 0
    W

    Even more simple solution:
    return str(bin(int(a, 2) + int(b, 2)))[2:]


  • 0
    C

    it is not suitable for longer or large numbers which over the range of integer


Log in to reply
 

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