周赛补题(AcWing、力扣)

力扣第87场周赛

第一题:2409. 统计共同度过的日子数 - 力扣(LeetCode)

思路:模拟。主要是要计算两个日期字符串之间的天数,对每个月的天数进行枚举然后叠加,注意要做一些特判。

代码

class Solution {
public:
    bool is(int a)
    {
        if(a == 4 || a == 6 || a == 9 || a == 11) return true;
        return false;
    }

    int days(string a, string b)
    {
        if(a > b) return 0;
        int sum = 0;
        int y1 = stoi(a.substr(0, 2)), y2 = stoi(b.substr(0, 2));
        int d1 = stoi(a.substr(3)), d2 = stoi(b.substr(3));
        for(int i = y1 + 1; i < y2; i ++)
        {
            if(i == 2) sum += 28;
            else if(is(i)) sum += 30;
            else sum += 31;
        }
        if(y1 == y2) sum += d2 - d1 + 1;
        else
        {
            sum += d2;
            if(y1 == 2) sum += 28 - d1 + 1;
            else if(is(y1)) sum += 30 - d1 + 1;
            else sum += 31 - d1 + 1;
        }
        return sum;
    }
    int countDaysTogether(string arriveAlice, string leaveAlice, string arriveBob, string leaveBob) {
        string s = max(arriveAlice, arriveBob), e = min(leaveAlice, leaveBob);
        return days(s, e);
    }
};

第二题:2410. 运动员和训练师的最大匹配数 - 力扣(LeetCode)

思路:排序+双指针。先将两个数组排序好,然后用双指针向后面进行移动并统计符合要求的数量。

代码

class Solution {
public:
    int matchPlayersAndTrainers(vector<int>& players, vector<int>& trainers) {
        sort(players.begin(), players.end());
        sort(trainers.begin(), trainers.end());
        int ans = 0, n = players.size(), m = trainers.size();
        for(int i = 0, j = 0; i < n && j < m;)
        {
            if(players[i] <= trainers[j])
            {
                ans ++;
                i ++, j ++;
            }
            else j ++;
        }
        return ans;
    }
};

力扣第311场周赛

第一题:2413. 最小偶倍数 - 力扣(LeetCode)

思路:如果n是奇数的话,那么2和n最小公倍数就是2*n,否则就是n。

代码

class Solution {
public:
    int smallestEvenMultiple(int n) {
        return n&1?2 * n:n;
    }
};

第二题:2414. 最长的字母序连续子字符串的长度 - 力扣(LeetCode)

思路:双指针。这题向大于求一个满足一个条件(第i个数-第i-1个数==1)子数组的最大值。可以直接通过双指针来进行模拟。

代码

class Solution {
public:
    int longestContinuousSubstring(string s) {
        int n = s.size();
        int ans = 0;
        for(int i = 0; i < n; i ++)
        {
            int j = i + 1;
            while(j < n && s[j] - s[j - 1] == 1) j ++;
            ans = max(ans, j - i);
            i = j - 1;
        }
        return ans;
    }
};

第三题:2415. 反转二叉树的奇数层 - 力扣(LeetCode)

思路:bfs。用队列来对树进行遍历,用数组存储奇数层的每个节点的值,同时用一个队列来存储奇数层的节点,将数组从后向前给给个节点重新赋值。

代码

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    TreeNode* reverseOddLevels(TreeNode* root) {
        queue<TreeNode*> q, qq;
        q.push(root);
        vector<int> res;
        int ans = 0;
        while(q.size())
        {
            int n = q.size();
            while(n --) 
            {
                auto p = q.front(); q.pop();
                if(ans & 1)
                {
                    res.push_back(p->val);
                    qq.push(p);
                }
                if(p->left) q.push(p->left);
                if(p->right) q.push(p->right);
            }
            if(ans & 1)
            {
                while(qq.size())
                {
                    auto p = qq.front(); qq.pop();
                    p->val = res.back(); res.pop_back();
                }
            }
            ans ++;
        }
        return root;
    }
};

第四题:2416. 字符串的前缀分数和 - 力扣(LeetCode)

思路:trie模板题。

代码

const int N = 1000010;
int son[N][26], idx;
int cnt[N];
class Solution {
public:
    void insert(string &a)
    {
        int p = 0;
        for(auto &i : a)
        {
            int u = i - 'a';
            if(!son[p][u]) son[p][u] = ++ idx;
            p = son[p][u];
            cnt[p] ++;
        }
    }

    int query(string &a)
    {
        int p = 0, sum = 0;
        for(auto i : a)
        {
            int u = i - 'a';
            if(!son[p][u]) return sum;
            p = son[p][u];
            sum += cnt[p];
        }
        return sum;
    }

    vector<int> sumPrefixScores(vector<string>& words) {
        memset(cnt, 0, sizeof cnt);
        memset(son, 0, sizeof son);
        idx = 0;
        for(auto &i : words) insert(i);
        vector<int> ans;
        for(auto &i : words)
            ans.push_back(query(i));
        return ans;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值