How can it be faster? (Python)


  • 0
    C

    Well I gave a similar code as others:

    class Solution(object):
        def canWinNim(self, n):
            """
            :type n: int
            :rtype: bool
            """
            return n % 4 != 0
    

    but it costed 48ms, on the middle of the distribution. I wonder how can it be faster? Shall I do something else to replace the % operation?


  • 3
    Z

    My 98% solution:

    class Solution(object):
        def canWinNim(self, n):
            """
            :type n: int
            :rtype: bool
            """
            return bool(n & 3)
    

  • 0
    J

    I tried the bitwise method, but it cost a even longer time than the modulo method...


  • 0
    Z

    @jianru It depends on the back end state. Just try another time and it may be very different


Log in to reply
 

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