a fast way use the c language


  • 0
    J

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <stdbool.h>

    int get_cell_num(int** grid, int gridRowSize, int gridColSize,int index_col,int index_row){

    int count = 0;
    
    
    //up
    if(index_row-1 >= 0){
    	
    	if(0 == grid[index_row-1][index_col]){
    		
    		count += 1;
    		
    	}
    }else {
    	count += 1;
    }
    
    //down
    if(index_row + 1 < gridRowSize){
    	
    	if(0 == grid[index_row+1][index_col]){
    		count += 1;
    	}
    	
    }else {
    	count += 1;
    	
    }
    
    //left
    if(index_col - 1 >= 0){
    	
    	if(0 == grid[index_row][index_col - 1]){
    		count += 1;
    	}
    	
    }else {
    	
    	count += 1;
    	
    }
    
    
    //right
    if(index_col + 1 < gridColSize){
    	
    	if(0 == grid[index_row][index_col + 1]){
    		
    		count += 1;
    	}
    	
    }else {
    	
    	count += 1;
    	
    }
    
    return(count);
    

    }


  • 0
    J

    int islandPerimeter(int** grid, int gridRowSize, int gridColSize) {

    int i = 0;
    int j = 0;
    int total_count = 0;
    for(i=0;i<gridRowSize;++i){
    	
    	for(j=0;j<gridColSize;++j){
    		
    			if(grid[i][j]){
    				
    					int value_node = get_cell_num(grid,gridRowSize,gridColSize,j,i);
    					total_count += value_node;
    				
    			}
    		
    	}
    	
    	
    }
    
    return(total_count);
    

    }


Log in to reply
 

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