码迷,mamicode.com
首页 > 其他好文 > 详细

463 Island Perimeter 岛屿的周长

时间:2018-04-21 19:45:13      阅读:97      评论:0      收藏:0      [点我收藏+]

标签:div   problems   c++   return   problem   empty   logs   script   rand   

详见: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 岛屿的周长

标签:div   problems   c++   return   problem   empty   logs   script   rand   

原文地址:https://www.cnblogs.com/xidian2014/p/8902394.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!