leetcode-162周赛-1252-奇数值单元格数目
作者:互联网
题目描述:
自己的提交:
class Solution: def oddCells(self, n: int, m: int, indices: List[List[int]]) -> int: nums = [[0] * m for _ in range(n)] for i,j in indices: for k in range(m): nums[i][k] += 1 for k in range(n): nums[k][j] += 1 res = 0 for i in range(n): for j in range(m): if nums[i][j] % 2 == 1: res += 1 return res
优化:
class Solution: def oddCells(self, n: int, m: int, indices: List[List[int]]) -> int: nums = [[0] * m for _ in range(n)] for i,j in indices: for k in range(m): nums[i][k] ^= 1 for k in range(n): nums[k][j] ^= 1 return sum(sum(row) for row in nums)
方法二:O(N)
class Solution: def oddCells(self, n: int, m: int, indices: List[List[int]]) -> int: a = [0]*n b = [0]*m for x, y in indices: a[x] ^= 1 b[y] ^= 1 return a.count(0)*b.count(1) + a.count(1)*b.count(0)
标签:count,周赛,nums,int,单元格,List,range,1252,indices 来源: https://www.cnblogs.com/oldby/p/11835616.html