Leetcode 岛屿问题 200, 1905, 694重新做,其他的想思路

200. Number of Islands

1.dfs

class Solution {
private:
    void dfs(vector>& grid, int i, int j){
        int m = grid.size(), n = grid[0].size();
        if(i<0 || j<0 || i>=m || j>=n) return;
        if(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);
    }
public:
    int numIslands(vector>& grid) {
        int res = 0;
        int m = grid.size(), n = grid[0].size();
        for(int i=0; i

这里把岛屿淹类似于维护了visited, 确保不会重复计算岛屿,dfs的作用是把一整块岛屿都淹没了,当遍历到岛屿的其中一块时, 会向外淹没,直到这个岛屿都被淹没

1254. Number of Closed Islands

class Solution {
private:
    void dfs(vector>& grid, int i, int j){
        int m = grid.size(), n = grid[0].size();
        if(i<0 || j<0 || i>=m || j>=n) return;
        if(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);
    }
public:
    int closedIsland(vector>& grid) {
        int res = 0;
        int m = grid.size(), n = grid[0].size();
        for(int i=0; i

去除掉四边的岛屿就可以

1020. Number of Enclaves

class Solution {
private:
    void dfs(vector>& grid, int i, int j){
        int m = grid.size();
        int n = grid[0].size();
        if(i<0 || j<0 || i>=m || j>=n) return;
        if(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);
    }
public:
    int numEnclaves(vector>& grid) {
        int move = 0; 
        int m = grid.size();
        int n = grid[0].size();
        for(int i=0; i

和上一题很像,这里就不需要把中间的岛屿淹没了,只要求1的数量就可以了

695. Max Area of Island

class Solution {
private:
    int dfs(vector>& grid, int i, int j){
        int m = grid.size(), n = grid[0].size();
        if(i<0 || j<0 || i>=m || j>=n) return 0;
        if(grid[i][j] == 0) return 0;
        grid[i][j] = 0;
        return dfs(grid, i-1, j) + dfs(grid, i+1, j) + dfs(grid, i, j-1) + dfs(grid, i, j+1) +1;
    }
public:
    int maxAreaOfIsland(vector>& grid) {
        int maxArea = 0;
        int m = grid.size(), n = grid[0].size();
        for(int i=0; i

1905. Count Sub Islands

class Solution {
private:
    void dfs(vector>& grid, int i, int j){
        int m = grid.size(), n = grid[0].size();
        if(i<0 || j<0 || i>=m || j>=n){
            return;
        }
        if(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);
    }
public:
    int countSubIslands(vector>& grid1, vector>& grid2) {
        int m = grid1.size(), n = grid1[0].size();
        int res = 0;
        for(int i=0; i

重要的是想到是什么是子岛屿

694. Number of Distinct Islands

class Solution {
private:
    void dfs(vector>& grid, int i, int j, string& s, int dir){
        int m = grid.size(), n = grid[0].size();
        if(i<0 || j<0 || i>=m || j>=n) return;
        if(grid[i][j] == 0) return;
        grid[i][j] = 0;
        s += to_string(dir) + ',';
        dfs(grid, i-1, j, s, 1);
        dfs(grid, i+1, j, s, 2);
        dfs(grid, i, j-1, s, 3);
        dfs(grid, i, j+1, s, 4);
        s += to_string(-dir) + ',';

    }
public:
    int numDistinctIslands(vector>& grid) {
        unordered_set record;
        int m = grid.size(), n = grid[0].size();
        for(int i=0; i

需要用一个set记录岛屿的形状

这里也要记录撤销的值

你可能感兴趣的:(leetcode,算法)