-
Notifications
You must be signed in to change notification settings - Fork 0
/
463.hpp
35 lines (30 loc) · 852 Bytes
/
463.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#ifndef LEETCODE_463_HPP
#define LEETCODE_463_HPP
#include <iostream>
#include <queue>
#include <algorithm>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <numeric>
#include <stack>
#include <string>
using namespace std;
class Solution {
public:
int islandPerimeter(vector<vector<int>> &grid) {
int area = 0, connect = 0;
for (int i = 0; i < grid.size(); ++i) {
for (int j = 0; j < grid[i].size(); ++j) {
area += grid[i][j];
if (i < grid.size() - 1 && grid[i][j] == 1 && grid[i + 1][j] == 1)
connect++;
if (j < grid[i].size() - 1 && grid[i][j] == 1 && grid[i][j + 1] == 1)
connect++;
}
}
return area * 4 - connect * 2;
}
};
#endif //LEETCODE_463_HPP