very easy one line answer


  • 1

    Since one can take [1, 2, 3] stones, we just need to determine if n is Multiple of 4.
    if the last stones is 4, the other one any of [1,2,3], we can take all left.

    bool canWinNim(int n) 
    {
        return (n & 3);  
    }

  • 0
    Z

    @ym65536 The solution still confuses me ><, can you explain that more? Thank you!


  • 1
    M

    这个题目只要把题目想清楚就行了。注意那句话:
    你和你的同学都非常聪明
    所以,如果n是4的倍数,无论你拿1~3,他只要拿4-你的个数就可以保证最后一个是他拿的。
    反之,如果n不是4的倍数,那么你第一次只要拿n%4就可以,接下来就是他拿几,你就拿4-几,这样一定能赢。哈哈


Log in to reply
 

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