zoukankan      html  css  js  c++  java
  • 2017/11/13 Leetcode 日记

    2017/11/13 Leetcode 日记

    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.

    (求给出格子的周长,注意有可能多个联通块)

     

    class Solution {
    public:
        int islandPerimeter(vector<vector<int>>& grid) {
            int row = grid.size(), col = grid[0].size();
            int count = 0, edge = 0;
            for(int i = 0; i < row; i++){
                for(int j = 0; j < col; j++){
                    if(grid[i][j]){
                        count ++;
                        if(i && grid[i-1][j]) edge ++;
                        if(j && grid[i][j-1]) edge ++;
                    }
                }
            }
            return 4 * count - 2 * edge;
        }
    };
    c++ 非递归实现
  • 相关阅读:
    锤炼自己,即便是铁,也要是铁中的佼佼者。
    数据库简单学习
    英雄总结
    强化肖龙
    神兽世界
    魔豆魔豆
    硬链接和软连接的区别
    深拷贝和浅拷贝的区别
    C++程序员(终身学习)
    矫健《圣徒》读后感
  • 原文地址:https://www.cnblogs.com/yoyo-sincerely/p/7824789.html
Copyright © 2011-2022 走看看