硬币排成线
有 n
个硬币排成一条线。两个参赛者轮流从右边依次拿走 1 或 2 个硬币,直到没有硬币为止。拿到最后一枚硬币的人获胜。
请判定 第一个玩家 是输还是赢?
您在真实的面试中是否遇到过这个题?
Yes
哪家公司问你的这个题? Airbnb Alibaba Amazon Apple Baidu Bloomberg Cisco Dropbox Ebay Facebook Google Hulu Intel Linkedin Microsoft NetEase Nvidia Oracle Pinterest Snapchat Tencent Twitter Uber Xiaomi Yahoo Yelp Zenefits
感谢您的反馈
样例
n = 1
, 返回 true
.
n = 2
, 返回 true
.
n = 3
, 返回 false
.
n = 4
, 返回 true
.
n = 5
, 返回 true
.
挑战
标签 Expand
O(1) 时间复杂度且O(1) 存储。
解题思路:
3个硬币一轮,后手拿必赢。
n%3!=0
public class Solution {
/**
* @param n: an integer
* @return: a boolean which equals to true if the first player will win
*/
public boolean firstWillWin(int n) {
// write your code here
return n%3!=0;
}
}