其他分享
首页 > 其他分享> > LeetCode #1252. Cells with Odd Values in a Matrix

LeetCode #1252. Cells with Odd Values in a Matrix

作者:互联网

题目

1252. Cells with Odd Values in a Matrix


解题方法

先构造这么一个矩阵出来,在构造的过程中判断当前位置是否为奇数,如果是就把总数+1,如果不是就把总数-1,最后返回奇数的总数。
时间复杂度:O(L(m+n)),L是indices的长度
空间复杂度:O(m
n)


代码

class Solution:
    def oddCells(self, n: int, m: int, indices: List[List[int]]) -> int:
        matrix = [[0] * m for _ in range(n)]
        oddnum = 0
        
        for indice in indices:
            r = indice[0]
            c = indice[1]
            for i in range(n):
                matrix[i][c] += 1
                if matrix[i][c] % 2:
                    oddnum += 1
                else:
                    oddnum -= 1
            for i in range(m):
                matrix[r][i] += 1
                if matrix[r][i] % 2:
                    oddnum += 1
                else:
                    oddnum -= 1
        
        return oddnum

标签:indice,matrix,int,Cells,oddnum,range,1252,indices,Matrix
来源: https://www.cnblogs.com/RatsCommander/p/14033944.html