[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 ...
随机推荐
- redis的相关原理
一.AOF 二.RDB 三.哨兵
- Jetson TX2
NVIDIA Jetson TX2作为一个嵌入式平台的深度学习端,具备不错的GPU性能,可以发现TX2的GPU的计算能力是6.2.这意味着TX2对半精度运算有着良好的支持,因此,完全可以在桌面端训练好 ...
- Idea破解到2100年的简单方法
第一步下载IntelliJ IDEA 2018.1.6版本,比这个更新的版本得你自己找注册码,因为旧的注册码对最新版本的软件不管用,所以建议还是下载这个版本,或者这个版本之前的也可以: 地址:http ...
- 《Mysql - 自增主键为何不是连续的?》
一:自增主键是连续的么? - 自增主键不能保证连续递增. 二:自增值保存在哪里? - 当使用 show create table `table_name`:时,会看到 自增值,也就是 AUTO_INC ...
- Integer.highestOneBit(int i)方法的作用与底层实现
在Integer类中有这么一个方法,你可以给它传入一个数字,它将返回小于等于这个数字的一个2的幂次方数.这个方法就是highestOneBit(int i). 比如下面的Demo,注意方法的输入与返回 ...
- hive学习(1)
什么是Hive Hive是基于Hadoop的一个数据仓库工具(E抽取T转换L加载),可以将结构化的数据文件映射为一张表,并提供类SQL查询功能. 本质是:将HQL转化成MapReduce程序 Hive ...
- c++ 通过sizeof运算符看内存对齐
一.基础数据类型 基础数据类型的sizeof,包括char.short,int,long,float,double 注意:实际数值有所偏差,与系统相关 二.数组及字符串 包括字符数组.字符指针.字符串 ...
- php将二维数组转换成我想要的一维数组
使用方法array_column($array,b,$arr): 参数说明:$array是我们原始的二维数组,B是我们想要的values,c是key 举个栗子:这是我原来的二维数组 这是我需要的一维数 ...
- 安装 go和beego后的环境变量设置
简介 之前有几次因为环境变量设置的问题 损失了一些时间,特在此做记录 安装golang 可参考官方Getting Started,但有时候有问题. 解压 tar -C /usr/local -xzf ...
- 【LEETCODE】57、数组分类,适中级别,题目:969、442、695
package y2019.Algorithm.array.medium; import java.util.ArrayList; import java.util.List; /** * @Proj ...