https://leetcode-cn.com/problems/cells-with-odd-values-in-a-matrix/
模拟大法好
class Solution:
def oddCells(self, n: int, m: int, indices: List[List[int]]) -> int:
grid=[[0 for i in range(m)] for i in range(n)]
for item in indices:
x,y=item[0],item[1]
for i in range(m):
grid[x][i]+=1
for i in range(n):
grid[i][y]+=1
ans=0
for item in grid:
for it in item:
if it%2!=0:
ans+=1
return ans;
只是效率低了点
https://leetcode-cn.com/problems/cells-with-odd-values-in-a-matrix/solution/ti-jie-1252-qi-shu-zhi-dan-yuan-ge-de-shu-mu-by-ze/
附上一个别人写的题解
class Solution:
def oddCells(self, n: int, m: int, indices: List[List[int]]) -> int:
rows = [0] * n
cols = [0] * m
for x, y in indices:
rows[x] += 1
cols[y] += 1
return sum((rows[x] + cols[y]) % 2 == 1 for x in range(n) for y in range(m))