zoukankan      html  css  js  c++  java
  • 1252. Cells with Odd Values in a Matrix

    Given n and m which are the dimensions of a matrix initialized by zeros and given an array indices where indices[i] = [ri, ci]. For each pair of [ri, ci] you have to increment all cells in row ri and column ci by 1.

    Return the number of cells with odd values in the matrix after applying the increment to all indices.

    分别统计行和列的次数,然后统计次数为奇数的行数r和列数c,那么答案是r*m + c*n - 2(r*c)

    class Solution(object):
        def oddCells(self, n, m, indices):
            """
            :type n: int
            :type m: int
            :type indices: List[List[int]]
            :rtype: int
            """
            row = [0] * n
            col = [0] * m
            for x in indices:
                row[x[0]] += 1
                col[x[1]] += 1
            r = 0
            c = 0
            for x in row:
                if x % 2 == 1:
                    r += 1
            for x in col:
                if x % 2 == 1:
                    c += 1
            print(r, c)
            return r * m + c * n - 2 * (r * c)
  • 相关阅读:
    pl sql 编程
    maven(一) maven到底是个啥玩意~
    Oracle 数据库管理员
    Oracle 常用函数
    Oracle 事务
    Oracle 分页
    java String类
    java 方法
    java 数组
    java 跨平台 数据类型 修饰符 程序结构
  • 原文地址:https://www.cnblogs.com/whatyouthink/p/13206714.html
Copyright © 2011-2022 走看看