Class Solution

java.lang.Object
g0201_0300.s0292_nim_game.Solution

public class Solution extends Object
292 - Nim Game.<p>Easy</p> <p>You are playing the following Nim Game with your friend:</p> <ul> <li>Initially, there is a heap of stones on the table.</li> <li>You and your friend will alternate taking turns, and <strong>you go first</strong>.</li> <li>On each turn, the person whose turn it is will remove 1 to 3 stones from the heap.</li> <li>The one who removes the last stone is the winner.</li> </ul> <p>Given <code>n</code>, the number of stones in the heap, return <code>true</code> <em>if you can win the game assuming both you and your friend play optimally, otherwise return</em> <code>false</code>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> n = 4</p> <p><strong>Output:</strong> false</p> <p><strong>Explanation:</strong></p> <pre><code> These are the possible outcomes: 1. You remove 1 stone. Your friend removes 3 stones, including the last stone. Your friend wins. 2. You remove 2 stones. Your friend removes 2 stones, including the last stone. Your friend wins. 3. You remove 3 stones. Your friend removes the last stone. Your friend wins. In all outcomes, your friend wins. </code></pre> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> n = 1</p> <p><strong>Output:</strong> true</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> n = 2</p> <p><strong>Output:</strong> true</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= n <= 2<sup>31</sup> - 1</code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • canWinNim

      public boolean canWinNim(int n)