Simple one line code (Python)


  • 0
    Z

    If the given number is less than 5,only 4 can make Nim win the game.
    Otherwise, whatever Nim takes (n) we can take 4-n makes the game back to the less-than-5 situation.
    For Nim,the game strategy is the same.
    Judging the num%4 is the key.

    class Solution(object):
        def canWinNim(self, n):
            """
            :type n: int
            :rtype: bool
            """
            return n>>2<<2!=n
    

    or

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

Log in to reply
 

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