标签:import self ons solution 统计 rom 服务 服务器 rem
题目描述:
自己的提交:
class Solution: def countServers(self, grid: List[List[int]]) -> int: from collections import Counter m,n = len(grid),len(grid[0]) falg = set() set1,set2 = Counter(),Counter() res = 0 for i in range(m): for j in range(n): if grid[i][j] == 1: if set1[i] > 0 or set2[j] > 0: res += 1 if set1[i] == 1: for x in falg: if x[0] == i: res += 1 falg.remove(x) break if set2[j] == 1: for x in falg: if x[1] == j: res += 1 falg.remove(x) break else: falg.add((i,j)) set1[i] += 1 set2[j] += 1 return res
优化:
class Solution: def countServers(self, grid: List[List[int]]) -> int: n,m=len(grid),len(grid[0]) col=[0]*m row=[0]*n for i in range(n): for j in range(m): if grid[i][j]: row[i]+=1 col[j]+=1 ans=0 for i in range(n): for j in range(m): if grid[i][j] and (col[j]>1 or row[i]>1): ans+=1 return ans
leetcode-164周赛-1267-统计参与通信的服务器
标签:import self ons solution 统计 rom 服务 服务器 rem
原文地址:https://www.cnblogs.com/oldby/p/11928705.html