leetcode 双周赛 Biweekly Contest 6

        最后一题比赛快结束的时候想到怎么做了(通过WA的数据猜出来的),比赛后10分钟做出来的。最终做了3题,时间1个小时左右吧。

1150. Check If a Number Is Majority Element in a Sorted Array

        这道题理论应该用二分,但是数据量很小(1000),所以就直接暴力过了:

class Solution {
public:
    bool isMajorityElement(vector<int>& nums, int target) {
        int count = 0;
        for(int i = 0;i < nums.size();i++)
        {
            if(nums[i] == target)
            {
                count++;
            }
            else if(nums[i] > target) break;
        }
        return count > (nums.size() / 2);
    }
};

1151 Minimum Swaps to Group All 1's Together

       这道题是滑动窗口题。找到包含最少0的窗口(窗口长度等于数组里1的个数)

class Solution {
public:
    int minSwaps(vector<int>& data) {
        int count = 0;
        int n = data.size();
        for(int i = 0;i < n;i++)
        {
            if(data[i] == 1) count++;
        }
        
        int begin = 0;
        int zeros = 0;
        int res = INT_MAX;
        for(int i = 0;i < n;i++) // end
        {
            int len = i - begin + 1;
            if(len > count)
            {
                if(data[begin] == 0) zeros--;
                begin++;
                len = count;
            }
            if(data[i] == 0) zeros++;
            if(len == count)
            {
                res = min(res, zeros);
            }
        }
        return res;
    }
};

1152. Analyze User Website Visit Pattern

        这道题可能是哈希和排序吧。WA了三次才过(有一次是没看清楚WA的点在哪,为了看错误点又提交了一次)。写的非常恶心。但是每次我洋洋洒洒写完100行的答案,总能在评论区看见10行搞定的 = =

class Solution {
public:
    vector<string> get(const string& s)
    {
        string str;
        vector<string> res;
        for(int i = 0;i < s.size();i++)
        {
            if(s[i] != ' ')
            {
                str.push_back(s[i]);
            }
            else
            {
                res.push_back(str);
                str.clear();
            }
        }
        return res;
    }
    bool IsSmaller(const string& s1, const string& s2)
    {
        vector<string> res1 = get(s1);
        vector<string> res2 = get(s2);
        for(int i = 0;i < res1.size();i++)
        {
            if(res1[i] == res2[i]) continue;
            return res1[i] < res2[i];
        }
        return false;
    }
    struct message
    {
        string username;
        int timestamp;
        string website;
        message() { }
        message(string& u, int& t, string& w) : username(u), timestamp(t), website(w){ }
        friend bool operator<(const message& m1, const message& m2)
        {
            return m1.timestamp < m2.timestamp;
        }
    };
    vector<string> mostVisitedPattern(vector<string>& username, vector<int>& timestamp, vector<string>& website) {
        unordered_map<string, int> count;
        int n = username.size();
        vector<message> msg(n);
        for(int i = 0;i < n;i++)
        {
            msg[i] = message(username[i], timestamp[i], website[i]);
        }
        sort(msg.begin(), msg.end());
        unordered_map<string, vector<string>> web;
        for(int i = 0;i < n;i++)
        {
            web[msg[i].username].push_back(msg[i].website);
        }
        for(auto& w : web)
        {
            string name = w.first;
            vector<string>& site = w.second;
            if(site.size() < 3) continue;
            unordered_set<string> unique;
            for(int i = 0;i < site.size(); i++)
            {
                for(int j = i + 1; j < site.size(); j++)
                {
                    for(int k = j + 1;k < site.size(); k++)
                    {
                        string str = site[i] + " " + site[j] + " " + site[k] + " ";
                        if(unique.find(str) == unique.end())
                        {
                            count[str]++;
                            unique.insert(str);
                        }
                    }
                }
            }
        }
        int maxtimes = 0;
        vector<string> res;
        string maxstr;
        for(auto& c : count)
        {
            int times = c.second;
            if(times > maxtimes)
            {
                maxtimes = times;
                maxstr = c.first;
            }
            else if(times == maxtimes)
            {
                if(IsSmaller(c.first, maxstr))
                {
                    maxstr = c.first;
                }
            }
        }
        return get(maxstr);
    }
};

1153. String Transforms Into Another String

        这道题首先不能一对多,存在就返回错误。然后如果存在转换存在闭环,如:

        abc -> bca

       相当于转换为a -> b -> c -> a,形成了一个回路,这种情况是可以转换的,因为可以借助不是abc的一个字符作为桥梁。

       但是如果闭环涵盖了26个小写字符,那么就没办法找到桥梁了,返回false。最终相当于判断所有形成闭环的字符是不是为26。(以上都是我从WA的答案中得到提示,才想到的

       总感觉求闭环数写的不是太对,我的方法是访问到已经访问过的字符后,把两个访问下标相减得到环路大小的,(我刚刚编的方法)。但是勉勉强强也AC了:

class Solution {
public:
    vector<int> visit;
    vector<int> graph;
    int res = 0;
    int count = 0;
    void dfs(int i) // find a loop
    {
        if(visit[i] != -1) 
        {
            res += count - visit[i];
            return;
        }
        visit[i] = count;
        if(graph[i] != -1)
        {
            count++;
            dfs(graph[i]);
        }
    }
    bool canConvert(string str1, string str2) {
        if(str1 == str2) return true;
        int n = str1.size();
        graph.resize(26, -1);
        for(int i = 0;i < n;i++)
        {
            if(graph[str1[i] - 'a'] != -1 && graph[str1[i] - 'a'] != str2[i] - 'a')
            {
                return false;
            }
            graph[str1[i] - 'a'] = str2[i] - 'a';
        }
        int count = 0;
        visit.resize(26, -1);
        for(int i = 0;i < 26;i++)
        {
            if(visit[i] == -1)
            {
                count = 0;
                dfs(i);
            }
        }
        if(res == 26) return false;
        return true;
    }
};

转载于:https://www.cnblogs.com/fish1996/p/11333624.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 内容概要 《计算机试卷1》是一份综合性的计算机基础和应用测试卷,涵盖了计算机硬件、软件、操作系统、网络、多媒体技术等多个领域的知识点。试卷包括单选题和操作应用两大类,单选题部分测试学生对计算机基础知识的掌握,操作应用部分则评估学生对计算机应用软件的实际操作能力。 ### 适用人群 本试卷适用于: - 计算机专业或信息技术相关专业的学生,用于课程学习或考试复习。 - 准备计算机等级考试或职业资格认证的人士,作为实战演练材料。 - 对计算机操作有兴趣的自学者,用于提升个人计算机应用技能。 - 计算机基础教育工作者,作为教学资源或出题参考。 ### 使用场景及目标 1. **学习评估**:作为学校或教育机构对学生计算机基础知识和应用技能的评估工具。 2. **自学测试**:供个人自学者检验自己对计算机知识的掌握程度和操作熟练度。 3. **职业发展**:帮助职场人士通过实际操作练习,提升计算机应用能力,增强工作竞争力。 4. **教学资源**:教师可以用于课堂教学,作为教学内容的补充或学生的课后练习。 5. **竞准备**:适合准备计算机相关竞的学生,作为强化训练和技能检测的材料。 试卷的目标是通过系统性的题目设计,帮助学生全面复习和巩固计算机基础知识,同时通过实际操作题目,提高学生解决实际问题的能力。通过本试卷的学习与练习,学生将能够更加深入地理解计算机的工作原理,掌握常用软件的使用方法,为未来的学术或职业生涯打下坚实的基础。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值