★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-bijwjyyy-kv.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
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.git
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.github
Example:微信
Input: Output: false Explanation: 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.4
你和你的朋友,兩我的一塊兒玩 Nim遊戲:桌子上有一堆石頭,每次大家輪流拿掉 1 - 3 塊石頭。 拿掉最後一塊石頭的人就是獲勝者。你做爲先手。函數
大家是聰明人,每一步都是最優解。 編寫一個函數,來判斷你是否能夠在給定石頭數量的狀況下贏得遊戲。spa
示例:code
輸入: 輸出: false 解釋: 若是堆中有 4 塊石頭,那麼你永遠不會贏得比賽; 由於不管你拿走 1 塊、2 塊 仍是 3 塊石頭,最後一塊石頭老是會被你的朋友拿走。4
1 class Solution { 2 func canWinNim(_ n: Int) -> Bool { 3 //若n=k*(m+1),則後取着勝 4 //反之,存在先取者獲勝的取法。n%(m+1)==0 ,先取者必敗 5 return n % 4 != 0 6 } 7 }