A simple C solution


  • 0
    B
    • I write this, but I am no sure whether it is depth-first traversal or no.
    int temp;
    int result;
    void Traversal(int** grid,int row,int col,int gridRowSize,int gridColSize)
    {
        if(grid[row][col]==0)
            return;
        grid[row][col]=0;
        temp++;
        if(row-1>=0)
            Traversal(grid,row-1,col,gridRowSize,gridColSize);
        if(row+1<gridRowSize)
            Traversal(grid,row+1,col,gridRowSize,gridColSize);
        if(col-1>=0)
            Traversal(grid,row,col-1,gridRowSize,gridColSize);
        if(col+1<gridColSize)
            Traversal(grid,row,col+1,gridRowSize,gridColSize);
    }
    int maxAreaOfIsland(int** grid, int gridRowSize, int gridColSize) {
        temp=0;
        result=0;
        int row=0,col=0;
        for(row=0;row<gridRowSize;row++)
        {
            for(col=0;col<gridColSize;col++)
            {
                if(grid[row][col]==1)
                {
                    Traversal(grid,row,col,gridRowSize,gridColSize);
                    if(result<temp)
                        result=temp;
                    temp=0;
                }
            }
        }
        return result;
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.