365天挑战LeetCode1000题——Day 129 第90场双周赛

2451. 差值数组不同的字符串

在这里插入图片描述

代码实现

class Solution {
public:
    string oddString(vector<string>& words) {
        vector<int> difference;
        difference.push_back(-1);
        auto str = words[0];
        int sz = str.size();
        for (int i = 1; i < sz; i++) {
            difference.push_back(str[i] - str[i - 1]);
        }
        for (int i = 1; i < words.size(); i++) {
            str = words[i];
            for (int j = 1; j < sz; j++) {
                if (difference[j] != str[j] - str[j - 1]) {
                    if (i >= 2) return str;
                    str = words[2];
                    for (int k = 1; k < sz; k++) {
                        if (difference[k] != str[k] - str[k - 1]) {
                            return words[0];
                        }
                    }
                    return words[1];
                }
            }
        }
        return "";
    }
};

2452. 距离字典两次编辑以内的单词

在这里插入图片描述

代码实现

class Solution {
private:
    int editDistance(string& query, string& key) {
        int count = 0;
        for (int i = 0; i < query.size() && count <= 2; i++) {
            if (query[i] != key[i]) {
                count++;
            }
        }
        return count;
    }
public:
    vector<string> twoEditWords(vector<string>& queries, vector<string>& dictionary) {
        vector<string> ans;
        for (auto& query : queries) {
            for (auto& key : dictionary) {
                if (editDistance(query, key) <= 2) {
                    ans.push_back(query);
                    break;
                }
            }
        }
        return ans;
    }
};

2453. 摧毁一系列目标

在这里插入图片描述

代码实现

class Solution {
public:
    int destroyTargets(vector<int>& nums, int space) {
        unordered_map<int, pair<int, int>> ump;
        for (auto& num : nums) {
            auto& [count, minNum] = ump[num % space];
            count++;
            minNum = (minNum == 0 ? num : min(minNum, num));
        }
        int ans = INT_MAX;
        int curCount = 0;
        for (auto it = ump.begin(); it != ump.end(); it++) {

            if (it->second.first >= curCount) {
                if (it->second.first == curCount) ans = min(ans, it->second.second);
                else ans = it->second.second;
                curCount = it->second.first;
            }
        }
        return ans;
    }
};

2454. 下一个更大元素 IV

在这里插入图片描述

代码实现

class Solution {
public:
    vector<int> secondGreaterElement(vector<int>& nums) {
        stack<int> stk1;
        priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
        int n = nums.size();
        vector<int> ans(n, -1);
        for (int i = 0; i < n; i++) {
            while (!pq.empty() && pq.top().first < nums[i]) {
                ans[pq.top().second] = nums[i];
                pq.pop();
            }
            while (!stk1.empty() && nums[stk1.top()] < nums[i]) {
                pq.push({nums[stk1.top()], stk1.top()});
                stk1.pop();
            }
            stk1.push(i);
        }
        return ans;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值