Leetcode #463 Javascript

pimkle發表於2018-11-27

ORIGINAL

/**
* @param {number[][]} grid
* @return {number}
*/
var islandPerimeter = function(grid) {
   let circle = 0;
   let x = grid.length;
   if(!x){
       return 0;
   }
   let y = grid[0].length;
   for(let j = 0; j < y; ++j){
       for(let i = 0; i < x; ++i){
           if(grid[i][j] === 1){
               circle += 4;
               if(j > 0 && grid[i][j - 1] === 1){
                   circle -= 1;
               }
               if(i < x - 1 && grid[i + 1][j] === 1){
                   circle -= 1;
               }
               if(j < y - 1 && grid[i][j + 1] === 1){
                   circle -= 1;
               }
               if(i > 0 && grid[i - 1][j] === 1){
                   circle -= 1;
               }
           }
       }
   }
   return circle;
}; 
複製程式碼

相關文章