其他分享
首页 > 其他分享> > 第79场双周赛

第79场双周赛

作者:互联网

这次的周赛总体的题目不难。。。虽然只做了前三道。应该是我个人的问题,困难的都没看,更何况写的是类。。。就没看困难题了。

第一题

题目链接:https://leetcode.cn/problems/check-if-number-has-equal-digit-count-and-digit-value/

个人题解:哈希表存一下,判断相等,个数增加,因为要全部满足,因此判断个数是否是字符串长度。

代码:

class Solution {
public:
    bool digitCount(string num) {
        unordered_map<char,int> hash;
        for(int i=0;i<num.size();i++) hash[num[i]-'0']++;
        
        int res=0;
        for(int i=0;i<num.size();i++){
            int t=num[i]-'0';
            if(t==hash[i]) res++;
        }
        return res==int(num.size());
    }
};

第二题

题目链接:https://leetcode.cn/problems/sender-with-largest-word-count/

个人题解:

  1. 哈希表存发送者和收到者,一个是字符串,一个是单词的个数。
  2. 写一个单词的个数函数。
  3. 用一个值来维护最大的次数,用一个字符串维护答案的最大值即可

代码:

class Solution {
public:
    int get(string s){
        int cnt=0,i=0;
        while(i<=s.size()){
            if(s[i]==' ') cnt++;
            i++;
        }
        cnt++;
        return cnt;
    }
    string largestWordCount(vector<string>& messages, vector<string>& senders) {
        unordered_map<string,int> hash;
        for(int i=0;i<senders.size();i++) hash[senders[i]]+=get(messages[i]);
        int maxnum=0;
        for(auto [k,v]:hash) maxnum=max(v,maxnum);
        string res;
        for(auto [k,v]:hash){
            if(v==maxnum){
                res=max(res,k);
            }
        }
        return res;
    }
};

第三题

题目链接:https://leetcode.cn/problems/maximum-total-importance-of-roads/

个人题解:

  1. 哈希表存入度和出度
  2. 贪心的思想,入度出度最小的排在前面。
  3. 算就可以了。

代码:

typedef long long LL;

class Solution {
public:
    LL maximumImportance(int n, vector<vector<int>>& roads) {
        unordered_map<int,int> hash;
        for(auto x:roads){
            hash[x[0]]++;
            hash[x[1]]++;
        }
        vector<array<int,2>> vec;
        for(auto x:hash) vec.push_back({x.second,x.first});
        
        sort(vec.begin(),vec.end());
        
        LL res=0;
        for(int i=vec.size()-1;i>=0;i--){
            res+=(LL)n*(vec[i][0]);
            n--;
        }
        return res;
    }
};

标签:hash,int,题解,LL,双周,vec,哈希,79
来源: https://www.cnblogs.com/cytcnblogs/p/16322746.html