一、单周赛303
1、2351.第一次出现两次的字母
(1)原题链接:力扣
https://leetcode.cn/problems/first-letter-to-appear-twice/
(2)解题思路:
用哈希表计数,同时判断当前数出现的次数为几次,若为一次则当前即为第二次出现,返回即可。
(3)参考代码:
class Solution {
public:
char repeatedCharacter(string s) {
unordered_map<char,int> mp;
for(auto & c : s) {
if(mp[c] == 1) return c;
else mp[c] ++;
}
return 0;
}
};
2、2352.相等行列对
(1)原题链接:力扣
https://leetcode.cn/problems/equal-row-and-column-pairs/
(2)解题思路:
第一步,将每个行和列用字符串数组保存,每个元素之间保存一个空格,避免出现歧义;
第二步,暴力遍历两个字符串数组,找到相同的字符串则cnt 加 1, 最终返回cnt即可。
(3)参考代码:
class Solution {
public:
int equalPairs(vector<vector<int>>& grid) {
int n = grid.size();
vector<string> col(n), row(n);
for(int i = 0; i < n; i ++) {
for(int j = 0; j < n; j ++) {
int x = grid[i][j];
string c = to_string(x);
col[i] += c;
col[i] += ' ';
}
}
for(int j = 0; j < n; j ++)
for(int i = 0; i < n; i ++) {
int s = grid[i][j];
string ch = to_string(s);
row[j] += ch;
row[j] += ' ';
}
int cnt = 0;
for(int i = 0; i < n; i ++) {
for(int j = 0; j < n; j ++) {
if(col[i] == row[j]) cnt ++;
}
}
return cnt;
}
};
3、2353.设计食物评分系统
(1)原题链接:力扣
https://leetcode.cn/contest/weekly-contest-303/problems/design-a-food-rating-system/
(2)解题思路:
第一步,用哈希表来保存每个食物、烹饪方式以及得分;
第二步,又因为如果存在并列,返回 字典序较小 的名字,所以,hash里面用set<pari<int, string>>来进行排序,又因为pair默认从小到大排序,所以在分值前加个负号;
第三步,删除步骤相对简单,详情见代码。
(3)参考代码:
class FoodRatings {
public:
unordered_map<string, set<pair<int, string>>> hash;
unordered_map<string, string> c;
unordered_map<string, int> r;
FoodRatings(vector<string>& foods, vector<string>& cuisines, vector<int>& ratings) {
for(int i = 0; i < foods.size(); i ++) {
c[foods[i]] = cuisines[i];
r[foods[i]] = ratings[i];
hash[cuisines[i]].insert({-ratings[i], foods[i]});
}
}
void changeRating(string food, int newRating) {
auto cuisine = c[food];
hash[cuisine].erase({-r[food], food});
r[food] = newRating;
hash[cuisine].insert({-newRating, food});
}
string highestRated(string cuisine) {
return hash[cuisine].begin()->second;
}
};
/**
* Your FoodRatings object will be instantiated and called as such:
* FoodRatings* obj = new FoodRatings(foods, cuisines, ratings);
* obj->changeRating(food,newRating);
* string param_2 = obj->highestRated(cuisine);
*/
二、双周赛83
1、2347.最好的扑克手牌
(1)原题链接:力扣
https://leetcode.cn/problems/best-poker-hand/
(2)解题思路:
由于数据范围很小,直接暴力干就完事。
(3)参考代码:
class Solution {
public:
string bestHand(vector<int>& ranks, vector<char>& suits) {
int n = ranks.size();
unordered_map<int, int> mp;
bool flag = true;
for(int i = 0; i < n - 1; i ++) {
if(suits[i] != suits[i + 1]) flag = false;
}
if(flag) return "Flush";
for(auto r: ranks) {
mp[r] ++;
}
for(auto [k, v]: mp) {
if(v >= 3) return "Three of a Kind";
}
for(auto [k, v]: mp) {
if(v == 2) return "Pair";
}
return "High Card";
}
};
2、2348.全 0 子数组的数目
(1)原题链接:力扣
https://leetcode.cn/problems/number-of-zero-filled-subarrays/
(2)解题思路:
第一步,先找出元素为0的子数组;
第二步,每个长度为k的子数组的方案数为k*(k - 1) / 2;
(3)参考代码:
class Solution {
public:
long long zeroFilledSubarray(vector<int>& nums) {
long long res = 0;
for(int i = 0; i < nums.size(); i ++ ) {
if(nums[i]) continue;
int j = i + 1;
while(j < nums.size() && !nums[j]) j ++;
res += (j - i) * (j - i + 1ll) / 2;
i = j;
}
return res;
}
};
3、2349.设计数字容器系统
(1)原题链接:力扣
https://leetcode.cn/problems/design-a-number-container-system/
(2)解题思路:
第一步,设计两个哈希表:第一个哈希表存储给定下标的数字;第二个哈希表存储当前数字所有下标的有序集合。
第二步,修改时,如果给定下标有数字,则在第二个哈希表中删除,然后再修改下标的数字,插入到对应数字的有序集合中。
第三步,查询时,直接返回集合中的最小值。
(3)参考代码:
class NumberContainers {
public:
unordered_map<int, set<int>> hash;
unordered_map<int, int> w;
NumberContainers() {
}
void change(int index, int number) {
if(w.count(index)) hash[w[index]].erase(index);
w[index] = number;
hash[number].insert(index);
}
int find(int number) {
if(!hash.count(number)) return -1;
auto& S = hash[number];
if(S.empty()) return -1;
return *S.begin();
}
};
/**
* Your NumberContainers object will be instantiated and called as such:
* NumberContainers* obj = new NumberContainers();
* obj->change(index,number);
* int param_2 = obj->find(number);
*/