力扣第316场周赛+Acwing第74场周赛补题

力扣

一.判断两个事件是否存在冲突

1.原题链接:力扣

2.解题思路:

        直接比较字符串即可,若event1[0] > event2[1] 或者 event2[0] > event1[1]说明两个字符串没有交集,返回false;否则返回true;

3.参考代码:

class Solution {
public:
    bool haveConflict(vector<string>& event1, vector<string>& event2) {
        if (event1[0] > event2[1] || event2[0] > event1[1]) {
            return false;
        }
        return true;
    }
};

二.最大公因数等于k的子数组数目

1.原题链接:力扣

2.解题思路:

        先枚举子数组的起点,再枚举子数组的终点,若子数组的最大公因数等于K,则ans++;否则跳出该子数组继续遍历

3.参考代码:

class Solution {
public:
    int gcd(int a,int b){
        return b > 0 ? gcd(b, a % b) : a;
    }
    
    int subarrayGCD(vector<int>& nums, int k) {
        int n = nums.size();
        int ans = 0;
       for(int i = 0; i < n; i++){
           int res = nums[i];
           for(int j = i; j < n; j++){
               res = gcd(res, nums[j]);
               if(res == k)ans++;
               else if(res < k)break;
            }
        }
        return ans;
    }
};

Acwing

一.最小身高差

1.原题链接:4707. 最小身高差 - AcWing题库

2.解题思路:

        分别计算每两个身高之间的差值,记录最小值;

3.参考代码:

#include<bits/stdc++.h>

using namespace std;

int main()
{
    int n; cin >> n;
    vector<int>a(n);
    for(auto & x : a)cin >> x;
    int ans = 10000;
    for(int i = 1; i < n; i++){
        ans = min(ans, abs(a[i] - a[i - 1]));
    }
    int res = abs(a[n - 1] - a[0]);
    if(res < ans)cout << res << endl;
    else cout << ans << endl;
    return 0;
}

二.立方体

1.原题链接:4708. 立方体 - AcWing题库

2.解题思路:

因为立方体有六个面,因此用深度优先搜索dfs模拟六个方位,dfs(t,x+1,y);dfs(t,x-1,y);dfs(t,x,y+1); dfs(t,x,y-1);dfs(t+1,x,y);dfs(t-1,x,y);如果越界或者是障碍物则直接输出ans,否则ans++;并将该方块设为障碍物;

3.参考代码:

#include<bits/stdc++.h>

using namespace std;

char a[11][11][11];
int k, n, m, ans;

void dfs(int t,int x,int y)
{
    if(a[t][x][y] == '#' || t<1 || x<1 || y<1 || t>k || x>n || y>m)return;
    ans++;
    a[t][x][y] = '#';
    dfs(t, x+1, y);
    dfs(t, x-1, y);
    dfs(t, x, y+1);
    dfs(t, x, y-1);
    dfs(t+1, x, y);
    dfs(t-1, x, y);
}

int main()
{
    int t = 1;
    cin >> k >> n >> m;
    for(int w = 1; w <= k; w++){
        for(int i = 1; i <= n; i++){
             for(int j = 1; j <= m; j++){
                 cin >> a[w][i][j];
             }
        }
    }
    int x, y;
    cin >> x >> y;
    dfs(t, x, y);
    cout << ans << endl;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值