c code


  • 0
    K

    Just add my c code, though it is a lot but it is very easy to understand since most code just repeating themselves. I'm new to those coding so maybe bad..

    void parseIsland(char** grid,int i,int j, int m, int n){
        if (i+1<=m-1)
        if(grid[i+1][j]=='0')
            ;
        else{
            grid[i+1][j]='0';
            parseIsland(grid,i+1,j,m,n);
        }
        if(j+1<=n-1)
        
        if(grid[i][j+1]=='0')
            ;
        else{
            grid[i][j+1]='0';
            parseIsland(grid,i,j+1,m,n);
        }
        if(j-1>=0)
        if(grid[i][j-1]=='0')
            ;
        else{
            grid[i][j-1]='0';
            parseIsland(grid,i,j-1,m,n);
        }
        if(i-1>=0)
        if(grid[i-1][j]=='0')
            ;
        else{
            grid[i-1][j]='0';
            parseIsland(grid,i-1,j,m,n);
        }
    }
    int numIslands(char** grid, int gridRowSize, int gridColSize) {
        int i,j;
        int count=0;
        for (i=0;i<=gridRowSize-1;i++)
            for(j=0;j<=gridColSize-1;j++)
            {
                if(grid[i][j]=='1'){
                    count++;
                    grid[i][j]=0;
                    parseIsland(grid,i,j,gridRowSize,gridColSize);
                    
                }
            }
        return count;
    }
    

Log in to reply
 

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