LeetCode之Nim Game
1、題目
You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last stone will be the winner. You will take the first turn to remove the stones.
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.
?
博弈論中極為經(jīng)典的尼姆游戲。有總數(shù)為n的石頭,每個人可以拿1~m個石頭,兩個人交替拿,拿到最后一個的人獲勝。究竟是先手有利,還是后手有利?
?
?
2、代碼實現(xiàn)
public class Solution {public boolean canWinNim(int n) {return n % 4 == 0 ? false : true; } }?
?
總結(jié)
以上是生活随笔為你收集整理的LeetCode之Nim Game的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LeetCode之Island Peri
- 下一篇: LeetCode之Max Consecu