1254. Number of Closed Islands 發表於 2023-02-13 | 分類於 leetcode problemsolution先將周圍島嶼淹沒,再去算剩下島嶼數 option 1 - dfs1234567891011121314151617181920212223242526272829303132333435class Solution {public: void dfs(vector<vector<int>>& grid, int i, int j){ int n = grid.size(), m = grid[0].size(); if(i<0 || j<0 || i>n-1 || j>m-1 || grid[i][j] == 1 ) return; grid[i][j] = 1; dfs(grid, i-1, j); dfs(grid, i+1, j); dfs(grid, i, j-1); dfs(grid, i, j+1); } int closedIsland(vector<vector<int>>& grid) { int n = grid.size(), m = grid[0].size(); for(int i=0;i<n;++i){ dfs(grid,i,0); dfs(grid, i, m-1); } for(int j=0;j<m;++j){ dfs(grid,0,j); dfs(grid, n-1, j); } int count = 0; for(int i=1;i<n-1;++i){ for(int j = 1 ;j<m-1;++j){ if(grid[i][j] == 0){ count++; dfs(grid, i, j); } } } return count; }}; option 2 - bfsanalysis time complexity O(m*n) space complexity O(m*n)