463. Island Perimeter
[[0,1,0,0],
[1,1,1,0],
[0,1,0,0],
[1,1,0,0]]
Answer: 16
Explanation: The perimeter is the 16 yellow stripes in the image below:
Last updated
Was this helpful?
[[0,1,0,0],
[1,1,1,0],
[0,1,0,0],
[1,1,0,0]]
Answer: 16
Explanation: The perimeter is the 16 yellow stripes in the image below:
Last updated
Was this helpful?
Was this helpful?
class Solution(object):
def islandPerimeter(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
count = repeat = 0
for i in range(len(grid)):
for j in range(len(grid[i])):
if(grid[i][j]):
count+= 1
# check left and up in this scenario
if (j!=0 and grid[i][j-1]):
repeat += 1
if (i !=0 and grid[i-1][j]):
repeat += 1
print("count: %s; repeat: %s"%(count,repeat))
return 4*count - 2*repeat