1 static int wing=[]() 2 { 3 std::ios::sync_with_stdio(false); 4 cin.tie(NULL); 5 return 0; 6 }(); 7 8 class Solution 9 { 10 public: 11 int islandPerimeter(vector<vector<int>>& grid) 12 { 13 int land=0,repeat=0; 14 for(int i=0;i<grid.size();i++) 15 { 16 for(int j=0;j<grid[i].size();j++) 17 { 18 if(grid[i][j]==1) 19 { 20 land++; 21 if(i!=0&&grid[i-1][j]==1) 22 repeat++; 23 if(j!=0&&grid[i][j-1]==1) 24 repeat++; 25 } 26 } 27 } 28 return (4*land-2*repeat); 29 } 30 };
计算总的陆地数和重叠的边数,利用数学关系计算即可。
浙公网安备 33010602011771号