码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 周赛补题(AcWing、力扣)


    AcWing第68场周赛

    第一题:4612. 去掉0 - AcWing题库

    思路:题目的意思可以理解为最先出现的1和最后出现的1之间有多少个0,可以找到第一次出现得1的位置和最后出现1的位置,在这两个位置之间枚举有多少的0。当然字符串中可能没有1,这说明需要删除的次数为0.

    代码

    1. #include <bits/stdc++.h>
    2. using namespace std;
    3. int main()
    4. {
    5. int t;
    6. cin >> t;
    7. while(t --)
    8. {
    9. string a;
    10. cin >> a;
    11. int n = a.size() - 1;
    12. int i = 0, j = a.size() - 1;
    13. while(i < n)
    14. {
    15. if(a[i] == '1') break;
    16. i ++;
    17. }
    18. while(j >= 0)
    19. {
    20. if(a[j] == '1') break;
    21. j --;
    22. }
    23. int ans = 0;
    24. if(i < j)
    25. {
    26. for(int k = i; k < j; k ++)
    27. if(a[k] == '0')
    28. ans ++;
    29. }
    30. cout << ans << endl;
    31. }
    32. return 0;
    33. }

    第二题:4613. 方格跳跃 - AcWing题库

    思路:在一个位置上为了能够跳出界外,只有两种情况

    1、从开头到该位置都是'<'

    2、从该位置到结尾都是'>'

    所以我们只用枚举向开头开始连续的'<'的位置和从结尾往前有多少个'>'.

    代码

    1. #include <bits/stdc++.h>
    2. using namespace std;
    3. const int N = 200010;
    4. char a[N];
    5. int n;
    6. int main()
    7. {
    8. cin >> n;
    9. cin >> a;
    10. int i = 0, j = n - 1;
    11. while(i < n)
    12. {
    13. if(a[i] == '>') break;
    14. i ++;
    15. }
    16. while(j >= 0)
    17. {
    18. if(a[j] == '<') break;
    19. j --;
    20. }
    21. int ans = i + n - 1 - j;
    22. cout << ans << endl;
    23. return 0;
    24. }

    力扣第301场周赛

    第一题:2404. 出现最频繁的偶数元素 - 力扣(LeetCode)

    思路:可以用map将偶数的个数存储起来,枚举map就最大次数的偶数即可。

    代码

    1. class Solution {
    2. public:
    3. int mostFrequentEven(vector<int>& nums) {
    4. map<int, int> mp;
    5. for(auto i : nums)
    6. if((i & 1) == 0)
    7. mp[i] ++;
    8. int ans = -1, cnt = 0;
    9. for(auto [x, y] : mp)
    10. {
    11. if(cnt < y) ans = x, cnt = y;
    12. else if(cnt == y) ans = min(ans, x);
    13. }
    14. return ans;
    15. }
    16. };

    第二题:2405. 子字符串的最优划分 - 力扣(LeetCode)

    思路:可以统计划分的次数,用一个set来存储,如果有一个数出现在set中就划分的次数就要加1.

    代码

    1. class Solution {
    2. public:
    3. int partitionString(string s) {
    4. int ans = 0;
    5. set<int> S;
    6. for(auto i : s)
    7. {
    8. if(S.count(i))
    9. {
    10. ans ++;
    11. S.clear();
    12. }
    13. S.insert(i);
    14. }
    15. ans ++;
    16. return ans;
    17. }
    18. };

    第三题:2406. 将区间分为最少组数 - 力扣(LeetCode)

    思路:可以用差分。这个题目的要求可以为在一个位置上面的次数的最大值。

    1. const int N = 1000010;
    2. class Solution {
    3. public:
    4. int a[N];
    5. int minGroups(vector<vector<int>>& intervals) {
    6. memset(a, 0, sizeof a);
    7. for(auto i : intervals)
    8. {
    9. int l = i[0], r = i[1];
    10. a[l] ++, a[r + 1] --;
    11. }
    12. for(int i = 1; i < N; i ++) a[i] += a[i - 1];
    13. int ans = 0;
    14. for(int i = 1; i < N; i ++)
    15. {
    16. ans = max(ans, a[i]);
    17. }
    18. return ans;
    19. }
    20. };

  • 相关阅读:
    【畅购商城】用户登录
    C++中promise和future详解
    做个清醒的程序员之拥抱AI
    工业级PoE交换机的工作原理
    探索低代码开发:编程新时代的开启
    Java设计模式之门面模式(Facade Pattern)
    MySQL报错 table “xxx” doesn‘t exit
    Github每日精选(第28期):Swift图像下载库 Kingfisher
    使用 Burpsuite 与 xray 进行联动
    汽车电子行业常见工具方法汇总
  • 原文地址:https://blog.csdn.net/qq_56407679/article/details/126872448
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号