Accepted C Solution with explanation


  • 13
    R
    bool canWinNim(int n) {
        return n%4;
    }
    

    well,the code is short and the important part is the idea behind the scene.
    according the example,if there are 4 stones,no matter what u do,u will lose. so as a smart guy u should leave 4 stone to yr friend. cos u can remove 3 stones at max, u'd win if there are 5~7 stones in the heap.

    how about 8 stones in the heap? after removed,5~7 stones will be left.and yr smart friend will win by drive u into the 4 stones situation.

    so u can win the game by counting the number of the stones, and make sure yr friend face the 4 stones situation.for example when n equal 15, u remove 3 stones,and 12 is 4 multiple 3.no matter how many stones yr friend removed, u can make it 8, and so on~~

    BTW this is a boring game,u can win when the stones can not be divide by 4.


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

  • 0
    J

    I think your submission is first one! Thanks!


  • 0
    R

    u're welcome. I think what u have learned by solving these problems matters most.


  • 0
    R

    another good solution


Log in to reply
 

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