[C++] Toooooo EASY SOLUTION (29ms)


  • 0
    B
    class Solution
    {
    public:
    	int maxRow,
    		maxCol,
    		maxTemp,
    		maxArea;
    
    	int maxAreaOfIsland(vector<vector<int>>& grid)
    	{
    		maxRow = grid.size(),
    		maxCol = grid[0].size(),
    		maxArea = 0;
    
    		for (int row = 0; row < maxRow; ++row)
    		{
    			for (int col = 0; col < maxCol; ++col)
    			{
    				if (grid[row][col] == 1)
    				{
    					maxTemp = 0;
    					searchRecursion(grid, row, col);
    
    					if (maxTemp > maxArea)
    					{
    						maxArea = maxTemp;
    					}
    				}
    			}
    		}
    
    		return maxArea;
    	}
    
    	void searchRecursion(vector<vector<int>>& grid, int row, int col)
    	{
    		if (grid[row][col] == 1)
    		{
    			++maxTemp;
    			grid[row][col] = 0;
    
    			if (col != 0)
    			{
    				if (grid[row][col - 1] == 1)
    				{
    					searchRecursion(grid, row, col - 1);
    				}
    			}
    			if (row != 0)
    			{
    				if (grid[row - 1][col] == 1)
    				{
    					searchRecursion(grid, row - 1, col);
    				}
    			}
    			if (col != maxCol - 1)
    			{
    				if (grid[row][col + 1] == 1)
    				{
    					searchRecursion(grid, row, col + 1);
    				}
    			}
    			if (row != maxRow - 1)
    			{
    				if (grid[row + 1][col] == 1)
    				{
    					searchRecursion(grid, row + 1, col);
    				}
    			}
    		}
    		else
    		{
    			return;
    		}
    	}
    };
    

    I'm not good at C++ yet. Just made an easy way....
    HAVE A NICE DAY :D


Log in to reply
 

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