当前位置: 首页 > news >正文

408算法题leetcode--第22天

200. 岛屿数量

  • 200. 岛屿数量
  • 时间:O(mn);空间:O(min(m, n)),队列最大入队个数,可以想象从左上到右下,第一次入队1个,第二次出队1,入队2,第三次出队2,入队3…
class Solution {
public:int dir[4][2] = {0, 1, 1, 0, 0, -1, -1, 0};  // 右,下,左,上int count = 0;int row;int column;void bfs(vector<vector<char>>& grid, int x, int y){queue<pair<int, int>>q;q.push({x, y});while(!q.empty()){auto t = q.front();q.pop();for(int i = 0; i < 4; i++){int new_x = t.first + dir[i][0], new_y = t.second + dir[i][1];if(new_x < 0 || new_x >= row || new_y < 0 || new_y >= column){continue;}if(grid[new_x][new_y] != '1'){continue;}grid[new_x][new_y] = '0';  // 访问q.push({new_x, new_y});}}}int numIslands(vector<vector<char>>& grid) {// bfsrow = grid.size(), column = grid[0].size();for(int i = 0; i < row; i++){for(int j = 0; j < column; j++){if(grid[i][j] == '1'){bfs(grid, i, j);++count;}}}return count;}
};

695. 岛屿的最大面积

  • 695. 岛屿的最大面积
  • 同上,bfs
class Solution {
public:int dir[4][2] = {0, 1, 1, 0, 0, -1, -1, 0};  // 右,下,左,上int ret = 0;int row;int column;int bfs(vector<vector<int>>& grid, int x, int y){grid[x][y] = 0;queue<pair<int, int>>q;q.push({x, y});int square = 1;while(!q.empty()){auto t = q.front();q.pop();for(int i = 0; i < 4; i++){int new_x = t.first + dir[i][0], new_y = t.second + dir[i][1];if(new_x < 0 || new_x >= row || new_y < 0 || new_y >= column){continue;}if(grid[new_x][new_y] != 1){continue;}grid[new_x][new_y] = 0;  // 访问++square;q.push({new_x, new_y});}}return square;}int maxAreaOfIsland(vector<vector<int>>& grid) {// bfsrow = grid.size(), column = grid[0].size();for(int i = 0; i < row; i++){for(int j = 0; j < column; j++){if(grid[i][j] == 1){int temp = bfs(grid, i, j);ret = max(ret, temp);}}}return ret;}
};

547. 省份数量

  • 547. 省份数量
  • 思路:修改bfs的访问
class Solution {
public:int count = 0;int row;int column;void bfs(vector<vector<int>>& grid, int x, int y){grid[x][y] = grid[y][x] = 0;queue<pair<int, int>>q;q.push({x, y});while(!q.empty()){auto t = q.front();q.pop();int new_x = t.first;for(int i = 0; i < column; i++){if(grid[new_x][i] == 0){continue;}grid[new_x][i] = grid[i][new_x] = 0;  // 访问q.push({i, new_x});}}}int findCircleNum(vector<vector<int>>& isConnected) {// bfsrow = isConnected.size(), column = isConnected[0].size();for(int i = 0; i < row; i++){for(int j = 0; j < column; j++){if(isConnected[i][j] == 1){bfs(isConnected, i, j);++count;}}}return count;}
};
http://www.lryc.cn/news/452221.html

相关文章:

  • dubbo微服务
  • 如何在 DAX 中计算多个周期的移动平均线
  • 微信小程序 图片的上传
  • 软件测试人员发现更多程序bug
  • Nagle 算法:优化 TCP 网络中小数据包的传输
  • C#入门教程
  • 【MySQL报错】---Data truncated for column ‘age‘ at row...
  • Go基础学习08-并发安全型类型-通道(chan)深入研究
  • some 蓝桥杯题
  • [linux 驱动]input输入子系统详解与实战
  • 2023_Spark_实验十:Centos_Spark Local模式部署
  • pyecharts-快速入门
  • vue3打包疯狂报错
  • STM32 软件触发ADC采集
  • Android SystemUI组件(08)睡眠灭屏 锁屏处理流程
  • C# 表达式与运算符
  • SpringBoot--最大连接数和最大并发数
  • CF687D Dividing Kingdom II 题解
  • 高空抛物AI检测算法:精准防控,技术革新守护城市安全
  • html+css+js实现Collapse 折叠面板
  • RM服务器研究(一)
  • 云岚到家xxl job 配置
  • 国内动态短效sk5
  • 【路径规划】路径平滑算法,A星算法拐点的圆弧化处理
  • 【寻找one piece的算法之路】——双指针算法!他与她是否会相遇呢?
  • UFS 3.1架构简介
  • 注册安全分析报告:科研诚信查询平台无验证方式导致安全隐患
  • 04.useTitle
  • ROS2中的srv、action、发布订阅三种方式
  • HarmonyOS/OpenHarmony 自定义弹窗页面级层级控制解决方案