一品网
  • 首页

1510. 石子游戏 IV SG定理即可


先找边界:SG[0] = 0表示当石子数为0时,先手输

然后套SG即可

若为n堆,每堆的SG异或即可

class Solution {
public:
    int SG[100001];
    int vis[100];



    bool winnerSquareGame(int n) {


        SG[0] = 0;
        for(int i = 1; i <= n; i++)
        {
            memset(vis, 0, sizeof(vis));
            for(int j = 1; j * j <= i; j++) vis[SG[i - j * j]] = 1;
            for(int j = 0; ; j++)
                if(!vis[j])
                {
                    SG[i] = j;
                    break;
                }
        }
        return SG[n];


    }
};
每日一题

相关


【LeetCode每日一题】员工的重要性

【LeetCode每日一题】整数反转

【python】Leetcode每日一题-森林中的兔子

【python】Leetcode每日一题-132模式

【每日一题】【链表】2021年11月20日-141. 环形链表

每日一题-Day31-删除链表的倒数第n个节点

每日一题·暑假 week1

【每日一题】【栈】【递归】【遍历】2021年12月1日-94. 二叉树的中序遍历

# 每日题目-1.6:每日一题+49+50

【每日一题】【list转int数组】【Lambda的简化-方法引用】2022年1月15日-NC45 实现二

【每日一题】【找到位置返回&升序数组中第K大就是n-K小】2022年1月17日-NC88 寻找第K

【每日一题】【map操作】【滑动窗口所需元素】2021年12月22日-76. 最小覆盖子串

标签

一品网 冀ICP备14022925号-6