- def fun389(grid):
- result = 0
- row = 0#同下
- for i in grid:
- square = 0#用于计数,以便找到遍历到哪一个格子里
- for j in i:
- if j == 1:
- if i[square - 1] == 0 and square != 0:
- result += 1
- elif row == 0:
- result += 1
- if grid[row - 1][square] == 0 and row != 0:
- result += 1
- elif square == 0:
- result += 1
- square += 1
- row += 1
- result *= 2
- return result
- '''
- 输入:[
- [0, 1, 0, 0],
- [1, 1, 1, 0],
- [0, 1, 0, 0],
- [1, 1, 0, 0]]
- 输出:16
- 输入:[
- [1, 1, 0, 0, 0, 0],
- [0, 1, 1, 1, 1, 1],
- [0, 1, 1, 1, 0, 0],
- [1, 1, 0, 0, 0, 0],
- [0, 1, 1, 1, 0, 0]]
- 输出:28
复制代码 |