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.

Example 1:

Input: n = 2, m = 3, indices = [[0,1],[1,1]]
Output: 6
Explanation: Initial matrix = [[0,0,0],[0,0,0]].
After applying first increment it becomes [[1,2,1],[0,1,0]].
The final matrix will be [[1,3,1],[1,3,1]] which contains 6 odd numbers.

Example 2:

Input: n = 2, m = 2, indices = [[1,1],[0,0]]
Output: 0
Explanation: Final matrix = [[2,2],[2,2]]. There is no odd number in the final matrix.

Constraints:

1 <= n <= 50
1 <= m <= 50
1 <= indices.length <= 100
0 <= indices[i][0] < n
0 <= indices[i][1] < m

python3:

 class Solution:
def oddCells(self, n: int, m: int, indices: List[List[int]]) -> int:
matrix = [[0] * m for _ in range(n)]
for ri, ci in indices:
for col in range(m):
matrix[ri][col] += 1
for row in range(n):
matrix[row][ci] += 1
rst = 0
for i in range(n):
for j in range(m):
if matrix[i][j] & 1:
rst += 1
return rst

[LeetCode]1252. Cells with Odd Values in a Matrix的更多相关文章

  1. 【leetcode】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 ind ...

  2. &lt;LeetCode OJ&gt; 328. Odd Even Linked List

    328. Odd Even Linked List Total Accepted: 9271 Total Submissions: 24497 Difficulty: Easy Given a sin ...

  3. LeetCode 328. 奇偶链表(Odd Even Linked List)

    328. 奇偶链表 328. Odd Even Linked List 题目描述 给定一个单链表,把所有的奇数节点和偶数节点分别排在一起.请注意,这里的奇数节点和偶数节点指的是节点编号的奇偶性,而不是 ...

  4. LeetCode 562. Longest Line of Consecutive One in Matrix(在矩阵中最长的连续1)$

    Given a 01 matrix M, find the longest line of consecutive one in the matrix. The line could be horiz ...

  5. [LeetCode] 378. Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素

    Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...

  6. 【LeetCode】329. Longest Increasing Path in a Matrix 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/longest- ...

  7. Leetcode:378. Kth Smallest Element in a Sorted Matrix

    题目: Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the ...

  8. 【Leetcode】【Medium】Search a 2D Matrix

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  9. 【leetcode刷题笔记】Search a 2D Matrix

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

随机推荐

  1. [译]使用BeautifulSoup和Python从网页中提取文本

    如果您要花时间浏览网页,您可能遇到的一项任务就是从HTML中删除可见的文本内容. 如果您使用的是Python,我们可以使用BeautifulSoup来完成此任务. 设置提取 首先,我们需要获取一些HT ...

  2. Node.js 多线程完全指南

    [原文] 很多人都想知道单线程的 Node.js 怎么能与多线程后端竞争.考虑到其所谓的单线程特性,许多大公司选择 Node 作为其后端似乎违反直觉.要想知道原因,必须理解其单线程的真正含义. Jav ...

  3. linux中安装robot环境

    https://www.cnblogs.com/lgqboke/p/8252488.html(文中验证robotframework命令应该为 robot --version) 可能遇到的问题: 1.p ...

  4. Python 推导式详解

    各种推导式详解 推导式的套路 之前我们已经学习了最简单的列表推导式和生成器表达式.但是除此之外,其实还有字典推导式.集合推导式等等. 下面是一个以列表推导式为例的推导式详细格式,同样适用于其他推导式. ...

  5. python基础(三)-- 文件操作

    一. 文件操作: 对文件操作流程 1.打开文件,得到文件句柄并赋值给一个变量 2.通过句柄对文件进行操作 3.关闭文件 现有文件如下 : Somehow, it seems the love I kn ...

  6. Zookeeper架构及FastLeaderElection机制

    原文链接:http://www.jasongj.com/zookeeper/fastleaderelection/ Zookeeper是什么 Zookeeper是一个分布式协调服务,可用于服务发现,分 ...

  7. java笔记--java的语言特性

    java的语言特性 1.简单性:例如:java不再支持多继承,而c++是支持多继承的,多继承比较复杂. c++中有指针,java中屏蔽了指针的概念.所以相对来说比较简单. //注:java语言的底层是 ...

  8. 学习C语言深入解剖笔记之关键字的秘密

    C语言关键字的秘密 重点说明: 1.if(略) 2.void ①C语言规定只有相同类型的指针才可以相互赋值 ②void*指针作为左值用于“接收”任意类型的指针 ③void*指针作为右值赋值给其它指针时 ...

  9. java.lang.ClassNotFoundException: org.springframework.boot.bind.RelaxedPropertyResolver 错误解决

    1.今天在搭建SpringBoot整合 pageHelper的时候报错如下 1.1 引入依赖如下: <!-- 分页插件 --> <dependency> <groupId ...

  10. Asp.netMVC中Ajax.BeginForm上传文件

    做一个上传并解压的功能,解压完了回调,解压多少文件.搞了半天用Ajax.BeginForm.各种坑,后来直接放弃 @using (Ajax.BeginForm("UploadFile&quo ...