1 Star 0 Fork 0

手捧向日葵的花语/力扣题集

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Project_2024_4_2_LiKou.txt 2.86 KB
一键复制 编辑 原始数据 按行查看 历史
//https://leetcode.cn/problems/number-of-enclaves/
//飞地的数量
class Solution {
public:
int dx[4] = {-1,1,0,0};
int dy[4] = {0,0,-1,1};
int numEnclaves(vector<vector<int>>& grid) {
int m = grid.size();
int n = grid[0].size();
vector<vector<int>> flag(m,vector<int>(n,0));
queue<pair<int,int>> q;
int ret = 0;
//int count = 0;
for(int i = 0; i < m; ++i)
{
for(int j = 0; j < n; ++j)
{
if(grid[i][j] == 1)
ret++;
}
}
for(int i = 0; i < m; ++i)
{
if(grid[i][0] == 1) {q.push({i,0}); flag[i][0] = 1; ret--; }
if(grid[i][n-1] == 1 && flag[i][n-1] != 1) {q.push({i,n-1}); flag[i][n-1] = 1; ret--;}
}
for(int i= 0; i < n; ++i)
{
if(grid[0][i] == 1 && flag[0][i] != 1) {q.push({0,i}); flag[0][i] = 1; ret--;}
if(grid[m-1][i] == 1 && flag[m-1][i] != 1) {q.push({m-1,i}); flag[m-1][i] = 1; ret--;}
}
cout << ret << endl;
while(q.size())
{
auto [a,b] = q.front();
q.pop();
for(int i = 0; i < 4; ++i)
{
int x = a+dx[i];
int y = b+dy[i];
if(x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 1 && flag[x][y] != 1)
{
q.push({x,y});
flag[x][y] = 1;
ret -= flag[x][y];
}
}
}
// for(int i = 0; i < m; ++i)
// {
// for(int j = 0; j < n; ++j)
// {
// if(flag[i][j] == 1)
// count++;
// }
// }
//return ret-count;
return ret;
}
};
//https://leetcode.cn/problems/map-of-highest-peak/
//地图中的最高点
class Solution {
public:
int dx[4] = {-1,1,0,0};
int dy[4] = {0,0,-1,1};
vector<vector<int>> highestPeak(vector<vector<int>>& isWater) {
int m = isWater.size();
int n = isWater[0].size();
vector<vector<int>> heigh(m,vector<int>(n,-1));
queue<pair<int,int>> q;
for(int i = 0; i < m; i++)
for(int j = 0; j < n; ++j)
if(isWater[i][j] == 1)
{
q.push({i,j});
heigh[i][j] = 0;
}
while(q.size())
{
auto [a,b] = q.front();
q.pop();
for(int i = 0; i < 4; ++i)
{
int x = a+dx[i];
int y = b+dy[i];
if(x >= 0 && x < m && y >= 0 && y < n && heigh[x][y] == -1)
{
q.push({x,y});
heigh[x][y] = heigh[a][b]+1;
}
}
}
return heigh;
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/a-chao-must-work-hard/li-kou-question-set.git
git@gitee.com:a-chao-must-work-hard/li-kou-question-set.git
a-chao-must-work-hard
li-kou-question-set
力扣题集
master

搜索帮助