# 岛屿外围Leetcode解决方案

## 问题陈述

### 使用案列

`grid = {{0,1,0,0},{1,1,1,0},{0,1,0,0},{1,1,0,0}}`
`16`
`grid = {{1}}`
`4`

## 方式（简单计数）

### Island Perimeter Leetcode解决方案的实现

#### C ++程序

```#include <bits/stdc++.h>

using namespace std;

int islandPerimeter(vector<vector<int>>& grid) {
int n = grid.size() , m = grid[0].size();
int perimeter = 0 , sides = 0;
for(int i = 0 ; i < n ; i++)
for(int j = 0 ; j < m;  j++) {
if(grid[i][j] == 1) {
sides = 0;
if(i == 0)
sides++;
else
sides += (grid[i - 1][j] == 0);

if(j == 0)
sides++;
else
sides += (grid[i][j - 1] == 0);

if(i == n - 1)
sides++;
else
sides += (grid[i + 1][j] == 0);

if(j == m - 1)
sides++;
else
sides += (grid[i][j + 1] == 0);

perimeter += sides;
}
}
return perimeter;
}

int main() {
vector< vector <int> > grid = {{0 , 1 , 0 , 0},
{1 , 1 , 1 , 0},
{0 , 1 , 0 , 0},
{1 , 1 , 0 , 0}};
cout << islandPerimeter(grid) << endl;
return 0;
}```

#### Java程序

```class island_perimeter {

public static void main(String args[]) {
int[][] grid = {{0 , 1 , 0 , 0},
{1 , 1 , 1 , 0},
{0 , 1 , 0 , 0},
{1 , 1 , 0 , 0}};
System.out.println(islandPerimeter(grid));
}

public static int islandPerimeter(int[][] grid) {
int n = grid.length , m = grid[0].length;
int perimeter = 0 , sides = 0;
for(int i = 0 ; i < n ; i++)
for(int j = 0 ; j < m;  j++) {
if(grid[i][j] == 1) {
sides = 0;
if(i == 0)
sides++;
else if(grid[i - 1][j] == 0)
sides++;

if(j == 0)
sides++;
else if(grid[i][j - 1] == 0)
sides++;

if(i == n - 1)
sides++;
else if(grid[i + 1][j] == 0)
sides++;

if(j == m - 1)
sides++;
else if(grid[i][j + 1] == 0)
sides++;

perimeter += sides;
}
}
return perimeter;
}
}```
`16`

### 孤岛边界Leetcode解决方案的复杂性分析

#### 时间复杂度

O（N * M） 其中N =网格中的行数，M =网格中的列数。

#### 空间复杂度

O（1） 因为我们为变量使用恒定空间。

## 方式（有效计数）

### Island Perimeter Leetcode解决方案的实现

#### C ++程序

```#include <bits/stdc++.h>

using namespace std;

int islandPerimeter(vector<vector<int>>& grid) {
int n = grid.size() , m = grid[0].size() , perimeter = 0;
for(int i = 0 ; i < n ; i++) {
for(int j = 0 ; j < m ; j++) {
if(grid[i][j] == 1) {
perimeter += 4;

if(i > 0 && grid[i - 1][j] == 1)
perimeter -= 2;

if(j > 0 && grid[i][j - 1] == 1)
perimeter -= 2;
}
}
}
return perimeter;
}

int main() {
vector< vector <int> > grid = {{0 , 1 , 0 , 0},
{1 , 1 , 1 , 0},
{0 , 1 , 0 , 0},
{1 , 1 , 0 , 0}};
cout << islandPerimeter(grid) << endl;
return 0;
}```

#### Java程序

```class island_perimeter {

public static void main(String args[]) {
int[][] grid = {{0 , 1 , 0 , 0},
{1 , 1 , 1 , 0},
{0 , 1 , 0 , 0},
{1 , 1 , 0 , 0}};
System.out.println(islandPerimeter(grid));
}

public static int islandPerimeter(int[][] grid) {
int n = grid.length , m = grid[0].length , perimeter = 0;
for(int i = 0 ; i < n ; i++) {
for(int j = 0 ; j < m ; j++) {
if(grid[i][j] == 1) {
perimeter += 4;

if(i > 0 && grid[i - 1][j] == 1)
perimeter -= 2;

if(j > 0 && grid[i][j - 1] == 1)
perimeter -= 2;
}
}
}
return perimeter;
}
}
```
`16`

### 孤岛边界Leetcode解决方案的复杂性分析

#### 时间复杂度

O（N * M） 其中N =网格中的行数，M =网格中的列数。

#### 空间复杂度

O（1） 因为我们为变量使用恒定空间。