LeetCode第261场周赛

2E46A3417181C5E2285BE8A1CAEC87ED

心情

题目在此
两题跪😭

第一题(转换字符串的最少操作次数)

class Solution {
public:
    int minimumMoves(string s) {
        int n = s.size();
        int res = 0;
        for (int i = 0; i < n; i ++){
            if (s[i] == 'O')continue;
            else res ++,i += 2;
        }
        return res;
    }
};

第二题(找出缺失的观测数据)

class Solution {
public:
    vector<int> missingRolls(vector<int>& r, int avg, int n) {
        int m = r.size();
        int Sum = avg*(n+m);
        vector<int> res;
        for (int i = 0; i < m; i ++)Sum -= r[i];
        if (Sum > n*6 || Sum < n)return res;
        int cnt = Sum%n;
        for (int i = 0; i < cnt; i ++)res.push_back(Sum/n+1);
        for (int i = cnt; i < n; i ++)res.push_back(Sum/n);
        return res;
    }
};

第三题(石子游戏 IX)

博 弈 博弈

第一个只能选1或2,那么就有一下选择情况
第一种: 1 , 1 , 2 , 1 , 2 , 1....... 1,1,2,1,2,1 ....... 1,1,2,1,2,1.......
第二种: 2 , 2 , 1 , 2 , 1 , 2....... 2,2,1,2,1,2 ....... 2,2,1,2,1,2.......

具体看代码,有注释

class Solution {
public:
    int d[3];
    //如果s0是偶数结果不变,否者就是翻转
    bool check(int s0, int s1, int s2) {  //选择顺序 s1,s1,s2,s1,s2,s1,s2
        if (s1 == 0) return false;
        if (s1 == 1) {  //特判
            if (s2 == 0) return false;
            return s0 % 2 == 0;
        } else {
            s1 -= 2;
            int s = min(s1, s2);
            s1 -= s;
            s2 -= s;
            //此时该Alice走下一步
            if (s1 == 0 && s2 == 0) return false;//这对应的是所有都选完没有出现选数和%3等于0
            if (s1 == 0) {
                if (s2 == 1) return false;      //此时无论下一步怎么选都不会%3等于0,所以也是必输
                return s0 % 2 == 0;   //在选第2个s2时一定输,所以输赢取决于s0的奇偶性
            } else {
                return s0 % 2 == 1;   //在选第1个s1时一定输,所以输赢也取决于so的奇偶性
            }
        }
    }
    bool stoneGameIX(vector<int>& s) {
        int n = s.size();
        for (int i = 0; i < n; i ++) d[s[i]%3] ++;
        return check(d[0], d[1], d[2]) || check(d[0], d[2], d[1]);	//两种情况都要考虑
    }
};

第四题(含特定字母的最小子序列)

待 续 待续

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值