463 Island Perimeter 岛屿的周长

Posted lina2014

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了463 Island Perimeter 岛屿的周长相关的知识,希望对你有一定的参考价值。

详见:https://leetcode.com/problems/island-perimeter/description/

C++:

class Solution {
public:
    int islandPerimeter(vector<vector<int>>& grid)
    {
        if (grid.empty() || grid[0].empty())
        {
            return 0;
        }
        int m = grid.size(), n = grid[0].size(), res = 0;
        for (int i = 0; i < m; ++i) 
        {
            for (int j = 0; j < n; ++j) 
            {
                if (grid[i][j] == 0)
                {
                    continue;
                }
                if (j == 0 || grid[i][j - 1] == 0)
                {
                    ++res;
                }
                if (i == 0 || grid[i - 1][j] == 0)
                {
                    ++res;
                }
                if (j == n - 1 || grid[i][j + 1] == 0)
                {
                    ++res;
                }
                if (i == m - 1 || grid[i + 1][j] == 0)
                {
                    ++res;
                }
            }
        }
        return res;
    }
};

 参考:https://www.cnblogs.com/grandyang/p/6096138.html

以上是关于463 Island Perimeter 岛屿的周长的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode 463. 岛屿的周长

LeetCode 463. 岛屿的周长 bfs

Leetcode-463 Island Perimeter

463. Island Perimeter

463. Island Perimeter

463. Island Perimeter