public class Solution {
public bool CanWinNim(int n) {
if(n < 4 || (n-1) % 4 == 0 || (n-2) % 4 == 0 || (n-3) % 4 == 0)
return true;
else
return false;
}
}
Here is an easy to understand solution, which is faster than 35% of implementations:
public bool CanWinNim(int n) {
return (n % 4) != 0;
}
Now, the next implementation optimized. It uses 3 = 0x000...0111 as a bitmask that is logically equivalent to the mod 4 operation, above:
public bool CanWinNim(int n) {
return (n & 3) != 0;
}
This algorithm is faster than 70% of implementations.
I don't think the solution can get much more efficient; so maybe performance depends on server load and garbage collection, etc. If you have a way, please let us know.