栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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 islandsStack = new List();
        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;
    }

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/301595.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号