[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 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的更多相关文章
- 【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 ...
- <LeetCode OJ> 328. Odd Even Linked List
328. Odd Even Linked List Total Accepted: 9271 Total Submissions: 24497 Difficulty: Easy Given a sin ...
- LeetCode 328. 奇偶链表(Odd Even Linked List)
328. 奇偶链表 328. Odd Even Linked List 题目描述 给定一个单链表,把所有的奇数节点和偶数节点分别排在一起.请注意,这里的奇数节点和偶数节点指的是节点编号的奇偶性,而不是 ...
- 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 ...
- [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 ...
- 【LeetCode】329. Longest Increasing Path in a Matrix 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/longest- ...
- 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 ...
- 【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 ...
- 【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 ...
随机推荐
- QT中常用工具总结
1.qmake 利用.pro文件生成Makefile 命令为: eg: qmake -o Makefile hello.pro 2. uic 利用ui界面审查.h头文件 命令为: eg: uic go ...
- Axure中继器设置单选
我们给元件添加组的名称是为了让软件知道哪些元件被放进了一个组中,然后软件会自动让这个组中只有一个元件能够是选中状态,以达到唯一被选中的效果.而中继器的这两个属性默认是启用的状态, 会把组的效果给取消, ...
- python3黑帽子渗透笔记第二章--网络基础
1 先来看看不可少的socket模块 (1)tcp_client.py 在渗透测试过程中,创建一个tcp客户端连接服务,发送垃圾数据,进行模糊测试等. (2)udp_client.py 2 nc工具的 ...
- springboot添加servlet的两种方法
1.是自己继承HttpServlet. 那么在类上面加 @WebServlet(loadOnStartup=1,urlPatterns={"/xxxServlet"}) 就可以了 ...
- Dubbo服务器与普通服务器的区别
Dubbo是一个阿里巴巴开源出来的一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案. 1.什么是分布式服务框架 分布式有两个特点,分别是内聚性和透明性(比如 ...
- url与uri区别
url属性: 是要求按照url的写法来写地址 URL:Uniform Resource Locator 统一资源定位符.它是可以唯一标识一个资源的位置 写法: http://localhost:808 ...
- java jdk 8u191 官网下载地址
目前jdk最后免费版本 jdk-8u191 下载地址: https://www.oracle.com/technetwork/java/javase/downloads/java-archive-ja ...
- 什么是渐进式Web App(PWA)?为什么值得关注?
转载自:https://blog.csdn.net/mogoweb/article/details/79029651 在开始PWA这个话题之前,我们先来看看Internet现状. 截至2017年1月, ...
- 《游戏引擎构架Game Engine Architecture》略读笔记
<游戏引擎构架Game Engine Architecture>略读笔记 分析标题作者 分析目录 选取感兴趣的章节阅读 25分钟略读完章节 分析标题作者 此书是一本帮助人入行做游戏的书,也 ...
- 《学渣的电子技术自学笔记》——三极管的放大区、截止区与饱和区(基于NPN型)
<学渣的电子技术自学笔记>--三极管的放大区.截止区与饱和区(基于NPN型) 1.放大区 三极管输出特性曲线近似水平的部分是放大区.在放大区,\(I_C=\overline{β}I_B ...