力扣
一.判断两个事件是否存在冲突
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;
}