Ruby solution


  • 0
    B
    def island_perimeter(grid)
    	num_of_land = down = right = 0
    	for gridrow in 0...grid.length
    		for gridrow_element in 0...grid[gridrow].length
    			if grid[gridrow][gridrow_element] == 1
        			num_of_land += 1
            		down += 1 if (grid[gridrow+1] != nil && grid[gridrow+1][gridrow_element] == 1)
            		right += 1 if (grid[gridrow][gridrow_element+1] != nil && grid[gridrow][gridrow_element+1] == 1)
    			else
    				next
    			end
    		end
    	end
    	return 4*num_of_land - 2*(down + right)
    end
    

Log in to reply
 

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