LeetCode695. 岛屿的最大面积(C#)

DFS经典题,两种方法,递归或者用栈

1.递归

public class Solution
 {
    public int MaxAreaOfIsland(int[][] grid) 
    {
        int rows = grid.Length;
        int cols = grid[0].Length;
        int res = 0;
        for(int i = 0; i < rows; i++)
        {
            for(int j = 0; j < cols; j++)
            {
                res = Max(res, DFS(grid, i, j, rows, cols));
            }
        }
        return res;
    }
    private int DFS(int[][] grid, int row, int col, int maxRow, int maxCol)
    {
        if(row < 0 || row >= maxRow ||  col < 0 || col >= maxCol || grid[row][col] == 0 )
            return 0;
        else
        {
            grid[row][col] = 0;
            return 1 + DFS(grid, row - 1, col, maxRow, maxCol) + DFS(grid, row + 1, col, maxRow, maxCol) + DFS(grid, row, col - 1, maxRow, maxCol) + DFS(grid, row, col + 1, maxRow, maxCol);
        }
    }
    private int Max(int a, int b)
    {
        return a > b? a : b;
    }

}

2.栈

public class Solution
 {
    public int MaxAreaOfIsland(int[][] grid) 
    {
        int rows = grid.Length;
        int cols = grid[0].Length;
        int res = 0;
        List<int[]> islandsStack = new List<int[]>();
        for(int i = 0; i < rows; i++)
        {
            for(int j = 0; j < cols; j++)
            {
                if(grid[i][j] != 0)
                {
                    islandsStack.Add(new int[2]{i,j});
                    int tempRes = 0;
                    while(islandsStack.Count != 0)
                    {
                        tempRes += 1;
                        int[] point = islandsStack[islandsStack.Count - 1];
                        islandsStack.RemoveAt(islandsStack.Count - 1);
                        grid[point[0]][point[1]] = 0;
                        if(point[0] + 1 < rows && grid[point[0] + 1][point[1]] != 0)
                        {
                            grid[point[0] + 1][point[1]] = 0;
                            islandsStack.Add(new int[2]{point[0] + 1, point[1]});
                        }
                        if(point[0]- 1 >= 0 && grid[point[0] - 1][point[1]] != 0)
                        {
                            grid[point[0] - 1][point[1]] = 0;
                            islandsStack.Add(new int[2]{point[0] - 1, point[1]});
                        }
                        if(point[1] + 1 < cols && grid[point[0]][point[1] + 1] != 0)
                        {
                            grid[point[0]][point[1] + 1] = 0;
                            islandsStack.Add(new int[2]{point[0], point[1]+ 1});
                        }
                        if(point[1] - 1 >= 0 && grid[point[0]][point[1] - 1] != 0)
                        {
                            grid[point[0]][point[1] - 1] = 0;
                            islandsStack.Add(new int[2]{point[0], point[1] - 1});    
                        }  
                    }
                    res = Max(res, tempRes);
                }
            }
        }
        return res;
    }
    private int Max(int a, int b)
    {
        return a > b? a : b;
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值