1 Star 0 Fork 0

蓝桥云课/python-100

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
090-perimeter.py 855 Bytes
一键复制 编辑 原始数据 按行查看 历史
xiaoyi733112 提交于 5年前 . python-100 answer
class Solution(object):
def island_perimeter(self, grid):
if grid is None:
raise TypeError('grid cannot be None')
sides = 0
num_rows = len(grid)
num_cols = len(grid[0])
for i in range(num_rows):
for j in range(num_cols):
if grid[i][j] == 1:
# Check left
if j == 0 or grid[i][j - 1] == 0:
sides += 1
# Check right
if j == num_cols - 1 or grid[i][j + 1] == 0:
sides += 1
# Check up
if i == 0 or grid[i - 1][j] == 0:
sides += 1
# Check down
if i == num_rows - 1 or grid[i + 1][j] == 0:
sides += 1
return sides
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/lanqiao-courses/python-100.git
git@gitee.com:lanqiao-courses/python-100.git
lanqiao-courses
python-100
python-100
master

搜索帮助