1906. 寻找比周围都大的点
1906. 寻找比周围都大的点
给一个n*m
大小的矩阵,寻找矩阵中所有比邻居(上下左右,对角也算,不考虑边界就是8
个咯)都严格大的点。
返回一个n*m
大小的矩阵,如果原矩阵中的点比邻居都严格大,则该位置为1
,反之为0
。
样例
样例 1
输入:
1 2 3
4 5 8
9 7 0
输出:
0 0 0
0 0 1
1 0 0
注意事项
1≤n,m≤100
import copy class Solution: """ @param grid: a matrix @return: Find all points that are strictly larger than their neighbors """ def highpoints(self, grid): cllist = copy.deepcopy(grid); rows = len(grid); colms = len(grid[0]) for item in range(0,rows): for valus in range(0,colms): flg = True; if item-1 >= 0: if valus-1 >= 0: if grid[item-1][valus-1] >= grid[item][valus]: flg = False; if grid[item-1][valus] >= grid[item][valus]: flg = False; if valus+1 <colms: if grid[item-1][valus+1] >= grid[item][valus]: flg = False; if valus-1 >= 0: if grid[item][valus-1] >= grid[item][valus]: flg = False; if valus + 1 < colms: if grid[item][valus+1] >= grid[item][valus]: flg = False; if item+1 < rows: if valus-1 >= 0: if grid[item+1][valus-1] >= grid[item][valus]: flg = False; if grid[item+1][valus] >= grid[item][valus]: flg = False; if valus+1 < colms: if grid[item+1][valus+1] >= grid[item][valus]: flg = False; cllist[item][valus] = 1 if flg else 0; return cllist;