AC C++ O(MN) Solution


  • 0
    S

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


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.