LeetCode 第317场周赛

  • A
class Solution {
public:
    int averageValue(vector<int>& nums) {
        int sum = 0;
        int num = 0;
        for(int d : nums){
            if(d % 6 == 0){
                sum += d;
                num++;
            }
        }
        if(num == 0) return 0;
        return sum / num;
    }
};
  • B
class Solution {
public:
    vector<vector<string>> mostPopularCreator(vector<string>& creators, vector<string>& ids, vector<int>& views) {
        map<string, long long> map;
        unordered_map<string, vector<pair<string, int>>> m;
        vector<pair<string, long long>> vecM;
        for(int i = 0; i < ids.size(); ++i){
            map[creators[i]] += views[i];
            m[creators[i]].push_back({ids[i], views[i]});
        }
        for(auto d : map){
            vecM.push_back({d.first, d.second});
        }
        
        sort(vecM.begin(), vecM.end(), [](pair<string, long long>& p, pair<string, long long>& q){
            return p.second > q.second;
        });
        
        vector<vector<string>> ans;
        long long max = (*vecM.begin()).second;
        for(auto d : vecM){
            if(d.second == max){
                sort(m[d.first].begin(), m[d.first].end(), [](pair<string, int>& p, pair<string, int>& q){
                    if(p.second == q.second) return p.first < q.first;
                    return p.second > q.second;
                });
                
                vector<string> temp;
                temp.push_back(d.first);
                temp.push_back((*m[d.first].begin()).first);
                ans.push_back(temp);
            }else{
                break;
            }
        }
        
        return ans;
    }
};
  • C
class Solution {
public:
    bool judge(long long n, int target){
        cout << n <<endl;
        int sum = 0;
        while(n != 0){
            sum += n % 10;
            n /= 10;
        }
        
        return sum <= target;
    }
    
    long long makeIntegerBeautiful(long long n, int target) {
        if(judge(n, target)) return 0;
        long long ans = 0;
        long long t = n;
        long long sum = 0;
        int time = 0;
        
        while(t != 0){
            sum =  t % 10 * pow(10, time) + sum;
            time++;
            t /= 10;
            cout << sum <<" ";
            if(judge(n + pow(10, time) - sum, target)){
                return pow(10, time) - sum;
            }
        }
        return ans;
    }
};
  • D
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值