Created
July 7, 2020 13:48
-
-
Save JyotinderSingh/2a3fe0c6d60789f9673c97cc7801926c to your computer and use it in GitHub Desktop.
Island Perimeter (LeetCode) | Algorithm Explanation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
int islandPerimeter(vector<vector<int>>& grid) { | |
int perimeter = 0; | |
for(int i = 0; i < grid.size(); ++i) { | |
for(int j = 0; j < grid[0].size(); ++j) { | |
if(grid[i][j]) { | |
perimeter += (i == 0 || grid[i - 1][j] == 0) + (i == grid.size() - 1 || grid[i + 1][j] == 0) + (j == 0 || grid[i][j - 1] == 0) + (j == grid[0].size() - 1 || grid[i][j + 1] == 0); | |
} | |
} | |
} | |
return perimeter; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment