1905. Count Sub Islands 發表於 2023-02-13 | 分類於 leetcode problemsolution12345678910111213141516171819202122232425262728293031323334353637class 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] == 0 ) return; grid[i][j] = 0; dfs(grid,i-1,j); dfs(grid, i+1,j); dfs(grid, i,j-1); dfs(grid, i,j+1); } int countSubIslands(vector<vector<int>>& grid1, vector<vector<int>>& grid2) { int n = grid1.size(), m = grid1[0].size(); for(int i=0;i<n;++i){ for(int j=0;j<m;++j){ // 如果grid2 的島嶼但不是grid1的島嶼,淹沒 // 這個島於肯定不是子島嶼,淹沒 if(grid1[i][j] == 0 && grid2[i][j] == 1) dfs(grid2, i, j); } } int count = 0; for(int i =0;i<n;i++){ for(int j=0;j<m;++j){ if(grid2[i][j] == 1){ count++; dfs(grid2, i, j); } } } return count; }}; analysis time complexity O(n*m) space complexity O(n*m)