zoukankan      html  css  js  c++  java
  • 463. Island Perimeter

    题目:

    You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represents water. Grid cells are connected horizontally/vertically (not diagonally). The grid is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells). The island doesn't have "lakes" (water inside that isn't connected to the water around the island). One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.

    Example:

    [[0,1,0,0],
     [1,1,1,0],
     [0,1,0,0],
     [1,1,0,0]]
    
    Answer: 16
    Explanation: The perimeter is the 16 yellow stripes in the image below:

    链接:https://leetcode.com/problems/island-perimeter/#/description

    3/27/2017

    150ms

     1 public class Solution {
     2     public int islandPerimeter(int[][] grid) {
     3         int rowNumber = grid.length;
     4         int colNumber = grid[0].length;
     5         int verticalCount = 0, horizonCount = 0;
     6 
     7         for (int i = 0; i < rowNumber; i++) {
     8             for (int j = 0; j < colNumber; j++) {
     9                 if ((j == 0 || grid[i][j-1] == 0) && grid[i][j] == 1) verticalCount++;
    10                 if ((i == 0 || grid[i-1][j] == 0) && grid[i][j] == 1) horizonCount++;
    11             }
    12         }
    13         return (verticalCount + horizonCount) * 2;
    14     }
    15 }

    网络不好,补上别人的算法

  • 相关阅读:
    怎样确定需求
    xampp进程和非进程执行
    将博客搬至CSDN
    管理心得
    数据库性能优化
    开发、架构总结
    activeMQ总结
    php数组操作函数
    Examples_08_08
    保险采购单的修复
  • 原文地址:https://www.cnblogs.com/panini/p/6629890.html
Copyright © 2011-2022 走看看