周赛补题

leetcode 单: 

第一题icon-default.png?t=M666https://leetcode.cn/problems/first-letter-to-appear-twice/哈希表进行统计即可

class Solution {
public:
    char repeatedCharacter(string s) {
        unordered_map<int, int> ans;

        for(int i = 0; i < s.size(); i ++){
            ans[s[i]] ++;

            if(ans[s[i]] == 2){
                return s[i];
            }
        }

        return 0;
    }
};

第二题icon-default.png?t=M666https://leetcode.cn/problems/equal-row-and-column-pairs/直接string暴力统计可以过

class Solution {
public:
    int equalPairs(vector<vector<int>>& grid) {
        int n = grid.size(), m = grid[0].size();
        vector<string> aa, bb;

        for(int i = 0; i < n; i ++){
            string x;
            for(int j = 0; j < m; j ++){
                x += grid[i][j];
            }
            
            aa.push_back(x);
        }

        for(int i = 0; i < m; i ++){
            string x;
            for(int j = 0; j < n; j ++){
                x += grid[j][i];
            }
            
            bb.push_back(x);
        }

        int count = 0;
        for(int i = 0; i < aa.size(); i ++){
            for(int j = 0; j < bb.size(); j ++){
                if(aa[i] == bb[j]){
                     count ++;
                }
            }
        }

        return count;
    }
};

第三题icon-default.png?t=M666https://leetcode.cn/problems/design-a-food-rating-system/维护一个 map<string, pair<int, string>> mp1,mp1[food] 表示 food 的评分的类型,再维护一个 map<string, set<pair<int, string>>> mp2,mp2[cuisine] 表示类型为 cuisine 的所有食物和评分。

class FoodRatings {
    typedef pair<int, string> pi;
    map<string, pi> mp1;
    map<string, set<pi>> mp2;

public:
    FoodRatings(vector<string>& foods, vector<string>& cuisines, vector<int>& ratings) {
        for (int i = 0; i < foods.size(); i++) {
            mp1[foods[i]] = pi(ratings[i], cuisines[i]);
            mp2[cuisines[i]].insert(pi(-ratings[i], foods[i]));
        }
    }
    
    void changeRating(string food, int newRating) {
        pi &p = mp1[food];
        mp2[p.second].erase(pi(-p.first, food));
        p.first = newRating;
        mp2[p.second].insert(pi(-p.first, food));
    }
    
    string highestRated(string cuisine) {
        return mp2[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);
 */

第四题icon-default.png?t=M666https://leetcode.cn/problems/number-of-excellent-pairs/可以举几个例子,会发现规律

结果的答案与计算num1和num2本身bit为 1 的位数和是一样的,所以我们可以用哈希表统计每个数本身bit为1的数量的数有多少个,最后可得出答案

class Solution {
public:
    long long countExcellentPairs(vector<int>& nums, int k) {
        sort(nums.begin(), nums.end());
        nums.erase(unique(nums.begin(), nums.end()), nums.end());

        unordered_map<int, int> ans;
        for(auto c : nums){
            int count = 0;

            while(c){
                count += c & 1;
                c >>= 1;
            }

            ans[count] ++;
        }

        long long sum = 0;
        for(int i = 0; i <= 32; i ++){
            for(int j = 0; j <= 32; j ++){
                if(i + j >= k)  sum += ans[i] * ans[j];
            }
        }

        return sum;
    }
};

leetcode 双: 

第一题icon-default.png?t=M666https://leetcode.cn/problems/best-poker-hand/简单模拟

class Solution {
public:
    string bestHand(vector<int>& ranks, vector<char>& suits) {
        set<char> a;
        for(auto c : suits){
            a.insert(c);
        }

        if(a.size() == 1)  return "Flush";

        map<int, int> b;
        for(auto c : ranks){
            b[c] ++;
        }

        int sum = 0;
        for(auto &[x, y] : b){
            sum = max(sum, y);
        }

        if(sum >= 3)  return "Three of a Kind";
        else if(sum == 2)  return "Pair";
        else  return "High Card";
    }
};

第二题icon-default.png?t=M666https://leetcode.cn/problems/number-of-zero-filled-subarrays/ 本来想找到所有连续0的长度再利用数学公式计算答案,但因为数据过大,需要用高精度了

class Solution {
public:
    long long zeroFilledSubarray(vector<int>& nums) {
        vector<int> ans;

        for(int i = 0; i < nums.size(); i ++){
            long long sum = 0;
            while(i < nums.size() && nums[i] == 0){
                sum ++;
                i ++;
            }

            ans.push_back(sum);
        }

        long long count = 0;
        for(auto c : ans){
            count += c * (c + 1) / 2;  //报错
        }

        return count;
    }
};

其实可以这样转换为加法运算 

class Solution {
public:
    long long zeroFilledSubarray(vector<int>& nums) {
        long long count = 0, sum = 0;
        for(auto c : nums){
            if(c == 0){
                sum ++;
                count += sum;
            }else{
                sum = 0;
            }
        }

        return count;
    }
};

第三题icon-default.png?t=M666https://leetcode.cn/problems/design-a-number-container-system/从前往后遍历第一个遍历到的number是下标最小的

class NumberContainers {
private:
    map<int, int>  mp;
public:
    NumberContainers() {

    }
    
    void change(int index, int number) {
        mp[index] = number;
    }
    
    int find(int number) {
        for (auto& [x, y] : mp) {
            if (y == number) return x;
        }
        return -1;
    }
};
/**
 * Your NumberContainers object will be instantiated and called as such:
 * NumberContainers* obj = new NumberContainers();
 * obj->change(index,number);
 * int param_2 = obj->find(number);
 */

第四题icon-default.png?t=M666https://leetcode.cn/problems/shortest-impossible-sequence-of-rolls/不断去找 1~k(作为一段),count段,答案就是 count + 1

当1~k都找到即找到了一段,count段,长度为count的子序列就都可以从原数组中得到

真的挺难想到这种思路

class Solution {
public:
    int shortestSequence(vector<int>& rolls, int k) {
        unordered_set<int> ans;

        int count = 0;
        for(int i = 0; i < rolls.size(); i ++){
            while(ans.size() < k && i < rolls.size()){
                ans.insert(rolls[i ++]);
            }

            if(ans.size() == k)  count ++;
            ans.clear();

            i = i - 1;
        }

        return count + 1;
    }
};

acwing :  

第一题icon-default.png?t=M666https://www.acwing.com/problem/content/4500/ 记得开 long long

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

int main(){
    int n;
    cin >> n;
    
    while(n --){
        long long a, b, c;
        cin >> a >> b >> c;
        
        cout << (a + b + c) / 2 << endl;
    }
    
    return 0;
}

第二题icon-default.png?t=M666https://www.acwing.com/problem/content/4501/ 数据范围比较小,可以用 dfs 直接枚举

#include <bits/stdc++.h>
using namespace std;

const int N = 20;
int aa[N], n;

bool dfs(int x, int y){
    if(x == n)  return y % 360 == 0;
    
    if(dfs(x + 1, y + aa[x]))  return true;
    if(dfs(x + 1, y - aa[x]))  return true;
    
    return false;
}

int main(){
    cin >> n;
    
    for(int i = 0; i < n; i ++)  cin >> aa[i];
    
    if(dfs(0, 0))  cout << "YES";
    else  cout << "NO";
    
    return 0;
}

第三题icon-default.png?t=M666https://www.acwing.com/problem/content/4502/ 数学知识,细分有三种情况

1、点再给定的圆外  2、与给定的圆的圆心重合  3、在给定的圆内(与圆心不重合)

#include <bits/stdc++.h>
using namespace std;

const double eps = 1e-8;

int cmp(double a, double b){
    if(fabs(a - b) < eps)  return 0;
    
    if(a > b)  return 1;
    else  return -1;
}

int main(){
    double r, x1, y1, x2, y2;
    scanf("%lf%lf%lf%lf%lf", &r, &x1, &y1, &x2, &y2);
    
    double dx = fabs(x1 - x2), dy = fabs(y1 - y2);
    double d = sqrt(dx * dx + dy * dy);
    
    if(cmp(r, d) <= 0)  printf("%lf %lf %lf", x1, y1, r);
    else{
        if(cmp(x1, x2) == 0 && cmp(y1, y2) == 0){
             printf("%lf %lf %lf", x1, y1 + r / 2, r / 2);
        }else{
            double rr = (r + d) / 2;
            double x = x2 + (x1 - x2) / d * rr;
            double y = y2 + (y1 - y2) / d * rr;
            
            printf("%lf %lf %lf", x, y, rr);
        }
    }
    
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值