Clean C++ soltuion


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

Log in to reply
 

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