Island Perimeter

2017/12/7 posted in  leetcode

class Solution {
public:
    int islandPerimeter(vector<vector<int>>& grid) {
        int squNum = 0 ,log = 0;
        for (int i = 0; i<grid.size(); i++) {
            for (int j = 0; j<grid[0].size(); j++) {
                if (grid[i][j]==1) {
                    squNum++;
                    if (i!=0&&grid[i-1][j]==1) {
                        log+=2;
                    }
                    if (j!=grid[0].size()-1&&grid[i][j+1]==1) {
                        log+=2;
                    }
                }
            }
        }
        return squNum*4-log;
    }
};