Both of you are very clever and have optimal strategies for the game. Write a function to determine whether you can win the game given the number of stones in the heap.
For example, if there are 4 stones in the heap, then you will never win the game: no matter 1, 2, or 3 stones you remove, the last stone will always be removed by your friend.
Credits:
public class Solution {
public boolean canWinNim(int n) {
// if (n <= 3) return true;
// boolean pre3 = true;
// boolean pre2 = true;
// boolean pre1 = true;
// for (int i = 4; i <= n; i++) {
// boolean cur = !pre3 || !pre2 || !pre1;
// pre3 = pre2;
// pre2 = pre1;
// pre1 = cur;
// }
// return pre1;
return !(n % 4 == 0);
}
}
没有评论:
发表评论