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;
}
};